Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 810
user695663 Avatar asked May 06 '11 16:05

user695663


People also ask

How do you get Li inside UL?

In JavaScript, you can use the . getElementsByTagName() method to get all the <li> elements in <ul>. In-addition, you can use the . querySelectorAll() method also to get all the <li>.

Does Li have to be in UL?

Answer: yes it does, but with: ol or menu, that's all!

How can get li id value in jquery?

ready(function() { $('#list li'). click(function() { alert($(this). attr("id")); alert($(this). text()); }); });


1 Answers

OLD simple answer: $('ul#parent li:eq(1)').index()

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

like image 117
pixelbobby Avatar answered Oct 21 '22 08:10

pixelbobby