Here’s the HTML:
<div id="star" class="star1">
<div id="star" class="star2">
<div id="star" class="star3">
<div id="star" class="star4">
<div id="star" class="star5">
</div>
</div>
</div>
</div>
</div>
And the Javascript:
$(document).ready(function() {
$('#star').click(function(e){
e.stopPropagation();
var class_name = $(this).attr('class');
alert(class_name);
});
});
The problem I’m having is I’m only ever getting the class of the first parent div.
I want get the class from whichever div is being clicked on. Is there a way of doing this?
The
idsof html element should be unique. Assign unique ids and use class selector and assigncommonclass to group them.Live Demo
The
approach you are using is not correct butsome time you are bound to use such false patterns if it is so you can do it this way.Live Demo