Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the element number/index between siblings [duplicate]

Tags:

How can you get the child number of the selected element, e.g.

<ul>     <li>First child</li>     <li>Second</li>     <li id="selected">Third child</li>     <li>Fourth child</li> </ul> 

Using something like this:

$('#selected').childNumber(); 

That should return "3" for it's parent wrap, regarding the child number.

like image 522
MacMac Avatar asked Jan 11 '11 03:01

MacMac


1 Answers

You can use .index() for this.

var selectedIndex = $("#selected").index() + 1; 

Since you have given an id for the li there is no need for the element selector with ul. You can directly use the id selector for the li element.

like image 122
rahul Avatar answered Oct 15 '22 15:10

rahul