Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining child index in it's parent [duplicate]

Tags:

jquery

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>
like image 818
farzan Avatar asked Dec 12 '10 10:12

farzan


1 Answers

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.

like image 86
Nick Craver Avatar answered Sep 28 '22 11:09

Nick Craver