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
  • 46,720
  • 11
  • 89
  • 119
farzan
  • 1,160
  • 2
  • 14
  • 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
  • 623,446
  • 136
  • 1,297
  • 1,155