Miller
Miller

Reputation: 744

Printing Id shows undefined in jquery

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

Answers (3)

Waqas Shahid
Waqas Shahid

Reputation: 1060

Instead of this use obj:

alert($(obj).attr('id'));

Upvotes: 0

Kumaresan K
Kumaresan K

Reputation: 146

First you will check your object name.

this will not work inside the function. You received the element as obj

Upvotes: 3

siva
siva

Reputation: 525

Change 'this' to 'obj'

function functionTest(obj){
alert("ok");
alert($(obj).attr('id'));
}

Upvotes: 7

Related Questions