how can i get order number of some element by javascript/jquery?
<ul> <li>Anton</li> <li class="abc">Victor</li> <li class="abc">Simon</li> <li>Adam</li> <li>Peter</li> <li class="abc">Tom</li> </ul>
There is 3xli with abc class. Now I need to get order(sequence) number of Simon li.
Thanks in advance
With Jquery's index() method
You can do it like this using a selector with .index()
, like this:
$('li:contains(Simon)').index('.abc'); //returns 1, it's 0 based //Or this... $('li').filter(':contains(Simon)').index('.abc'); //returns 1
Without the selector, you'd get 2
, the index of the <li>
in the parent overall, regardless of the class. You can view a quick demo here. Keep in mind it's a 0 based index, may need to + 1
the result for display in some cases, depends what you need it for.
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