Reputation: 85
Here it is demo of my code. I just want to show Id of button while it is saying undefined
Code:
$('#btn').on('click', function () {
var $this = $(this);
var ID = $(this).id;
alert(ID);
});
Upvotes: 0
Views: 115
Reputation: 402
try this and you will get button id-
$('#btn').on('click', function () {
alert(this.id);
});
second method-
$('#btn').click(function () {
alert(this.id);
)};
After Update - Please Try
Upvotes: 0
Reputation: 10179
Use $.prop()
.
$('#btn').on('click', function () {
alert($(this).prop("id"));
});
Use this.id
.
$('#btn').on('click', function () {
alert(this.id);
});
Upvotes: 0
Reputation: 992
You can use .attr(attribute)
.
$('#btn').on('click', function () {
var ID = $(this).attr('id');
alert(ID);
});
Upvotes: 0
Reputation: 1506
What about this:
$(".a").click(function(){
var id = this.id;
alert(id);
});
Upvotes: 1
Reputation:
Demo http://jsfiddle.net/vN88X/8/
$('#btn').click(function () {
alert($(this).attr('id'));
});
Upvotes: 1
Reputation: 68566
You've got it a little mixed up - you need to be using this.id
. There is no id
"method" in jQuery, however getting it via. .attr('id')
is possible (although there's no real need for that).
Upvotes: 2
Reputation: 1288
It Should be
$('#btn').on('click', function () {
var ID = $(this).attr('id');
alert(ID);
});
Here is Updated Demo http://jsfiddle.net/vN88X/3/
Upvotes: 0
Reputation: 353
Try
$(this).attr("id");
You will get the "id" attribute of the element.
Upvotes: 0
Reputation: 7063
The right code is the following :
$('#btn').click(function () {
var ID = $(this).attr('id');
alert(ID);
});
Have a look to the fiddle : http://jsfiddle.net/vN88X/6/
Upvotes: 0
Reputation: 150253
Simply use the DOM native functions and properties:
$('#btn').on('click', function () {
alert(this.id);
});
If you decided you want to use jQuery for the overkill:
alert($(this).attr('id'));
Upvotes: 2