Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Get numeric position/index of spcific <li> in an unordered list

I have a simple unordered list and would like to find the numeric index/position of a specific element.

e.g:

<ul id="mylist">
    <li id="id_a">Content</li>
    <li id="id_b">Content</li>
    <li id="id_c">Content</li>
    <li id="id_d">Content</li>
    <li id="id_e">Content</li>
</ul>

Pseudo code:

$('ul#mylist').getNumericIndexOf('id_c'); // 2
$('ul#mylist').getNumericIndexOf('id_a'); // 0
$('ul#mylist').getNumericIndexOf('id_e'); // 4

Any input is much appreciated!

like image 857
n00b Avatar asked Mar 16 '23 03:03

n00b


1 Answers

Use index():

DEMO

$("#id_c").index();
like image 108
mattytommo Avatar answered Mar 18 '23 17:03

mattytommo