Reputation: 3321
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?
Upvotes: 0
Views: 138
Reputation: 10403
try this:
$(".myClass").click(function(e){
alert(this.id);
}
or
$(".myClass").click(function(e){
alert($(this).attr("id"));
}
Upvotes: 4
Reputation: 22485
$(".myClass").click(function(e){
alert($(this).attr('id'));
}
should do the trick!!
jim
Upvotes: 4