Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example jquery get index/position of sibling

Let's say I have the following html

<ul>
  <li id="a">a</li>
  <li id="b">b</li>
  <li id="c">c</li>
</ul>
<ul>
  <li id="d">d</li>
  <li id="e">e</li>
  <li id="f">f</li>
</ul>

If I used jQuery to grab the item $('#e'), how do I determine the position or position/index of e relative to its siblings d and f? In other words, I'm expecting the value 1 (if zero based index) to be returned or 2 (if one based index) because it is the second element in the ul list.

like image 483
John Avatar asked Oct 19 '12 03:10

John


1 Answers

Simple: var idx = $('#e').index() // zero based

like image 167
elclanrs Avatar answered Sep 27 '22 17:09

elclanrs