Alex Rivera | Logout

How to get the index of an <li> in a <ul>

Asked 2011-05-06T16:11:36.583
25

Possible Duplicates:
How to get index of <li> element
jQuery - get the index of a element with a certain class

I have:

<ul id="parent">
     <li id="li1">li1</li>
     <li id="li2">li2</li>
     <li id="li3">li3</li>
</ul>

There are some other <ul> and <li> tags elsewhere.

I want to get the index of li2 which is in the <ul> with id parent using jQuery

Edit
Report

2 Answers

12

Use .index():

$('#li2').index();

IDs have to be unique so in case they are not in your HTML, you better fix this (e.g. by using classes).

answered 2011-05-06T16:15:54.650
3
var index = $("#li2").prevAll().length;  //assuming 0 based index
answered 2011-05-06T16:16:30.907

Your Answer