3

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..

Miller
  • 744
  • 3
  • 15
  • 39
  • Check this post http://stackoverflow.com/questions/5586857/inline-onclick-javascript-variable –  Jan 09 '16 at 12:25

3 Answers3

7

Change 'this' to 'obj'

function functionTest(obj){
alert("ok");
alert($(obj).attr('id'));
}
siva
  • 525
  • 2
  • 14
3

First you will check your object name.

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

Kumaresan K
  • 146
  • 6
0

Instead of this use obj:

alert($(obj).attr('id'));
Waqas Shahid
  • 1,051
  • 1
  • 7
  • 22