18

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>
ajreal
  • 45,869
  • 10
  • 83
  • 118
farzan
  • 1,140
  • 2
  • 15
  • 25

1 Answers1

32

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.

Nick Craver
  • 610,884
  • 134
  • 1,288
  • 1,151