tags:

views:

41

answers:

2

Hi all,

I am trying to grab the ID of a DIV class referenced by its Class name using JQuery, but failed to do it.

$(".myClass").click(function(e){
alert($(this).id);
}

<DIV id="myTest1" class="myClass">
Helloworld I am new to JQuery 1
</DIV>

   <DIV id="myTest2" class="myClass">
    Helloworld I am new to JQuery 2
    </DIV>

Could you help me please?

+3  A: 
$(".myClass").click(function(e){
    alert($(this).attr('id'));
}

should do the trick!!

jim

jim
Thank you Jim for your quick help.
+3  A: 

try this:

$(".myClass").click(function(e){
    alert(this.id);
}

or

$(".myClass").click(function(e){
    alert($(this).attr("id"));
}
Am
Many thanks to Am for your help. But I have to select Jim since he came first for this question.