Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element order number

Tags:

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

like image 337
user270158 Avatar asked Apr 09 '10 08:04

user270158


2 Answers

With Jquery's index() method

like image 89
Sarfraz Avatar answered Sep 22 '22 03:09

Sarfraz


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.

like image 20
Nick Craver Avatar answered Sep 21 '22 03:09

Nick Craver