...">

Jquery index in parent class

I need a way to find the index number of one child.

here is its CSS

<div class="parent"> <div class="son"></div> <div class="son"></div> <div class="son"></div> <div class="son"></div> <div class="son"></div> </div> <div class="parent"> <div class="son"></div> <div class="son"></div> <div class="son"></div> <div class="son"></div> <div class="son"></div> </div> 

My jquery code is like this

 var number = $(".son").index(this); 

When I use this code, it will consider the son as a whole. For example, when I click on the second child in the second parent class, it will give me the number var 7. I want the son class to always start counting from zero.

+4
source share
2 answers

Try the following:

 $(this).parent().find('.son').index(this) 

As other participants note:

 $(this).index() 

Does the task run, since index () without arguments returns the position of the element relative to its siblings.

Documentation:

If the argument is not passed to the .index () method, the return value is an integer indicating the position of the first element in the jQuery object relative to its sibling elements.

+9
source

This should do it:

$(this).index()

Try it here:

http://jsfiddle.net/uKa7d/

+1
source

Source: https://habr.com/ru/post/1443094/


All Articles