Valamas
Valamas

Reputation: 24729

Cannot get id of link clicked

I am getting id is undefined. http://jsfiddle.net/valamas/YUPWu/

I am hoping someone will pick up my (trivial?) error.

thanks

Upvotes: 0

Views: 62

Answers (2)

Rab
Rab

Reputation: 35572

Its because this is inaccessible. DEMO

$(function ()
{
    $(document).on('click', "#MyId", function () { MyId_Click(this); });
});
function MyId_Click(obj)
{
    var theId = $(obj).attr('id'); 
    alert(theId);
}
​

Upvotes: 2

wirey00
wirey00

Reputation: 33661

It's because $(this) is referring to nothing? $(this) usually means your selected item.. which in your case is nothing since inside that function it's not pointing to any element. You can do it like this

$(function (){
   $(document).on('click', "#MyId", function () { 
       var theId = $(this).prop('id'); //$(this).id does not work either.
       alert(theId);
   });
});

http://jsfiddle.net/YUPWu/1/

Upvotes: 3

Related Questions