How can I determine index of an element in it's parent? Assume you have following DOM structure and you have set a click event listener for child divs. When each of them is clicked, I want to know it's index regarding the parent div.
<div class="parent">
<div class="child">...</div>
<div class="child">...</div>
<div class="child">...</div>
<div class="child">...</div>
</div>
To get an index in of an elements in its parent (amongst siblings really) use .index()
without any parameters, for example:
$(".child").click(function() {
alert("Index: " + $(this).index());
});
You can test it out here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With