user327712
user327712

Reputation: 3321

How to grab the ID of a DIV tag referenced by the Class ID instead

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

Answers (2)

Roman
Roman

Reputation: 10403

try this:

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

or

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

Upvotes: 4

jim tollan
jim tollan

Reputation: 22485

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

should do the trick!!

jim

Upvotes: 4

Related Questions