Reputation: 744
I am calling a javascript function from my onclick passing this keyword as parameters.I need to print id of used in the div.But it shows undefined.
<div class="btn-group" tabindex="0"><a class="btn active btn-danger" onclick="functionTest(this)" id="male" >Male</a><a class="btn btn-default" onclick="functionTest(this)" id="female">Female</a></div>
below is my function
function functionTest(obj){
alert("ok");
alert($(this).attr('id'));
}
I am not getting my id,IT SHOWS UNDEFINED
.
Can anybody help me any help will be highly appreciable..
Upvotes: 3
Views: 121
Reputation: 146
First you will check your object name.
this will not work inside the function. You received the element as obj
Upvotes: 3
Reputation: 525
Change 'this' to 'obj'
function functionTest(obj){
alert("ok");
alert($(obj).attr('id'));
}
Upvotes: 7