tags:

views:

35

answers:

1

I am trying to find the position of Test3 in jquery can someone lead me down the right path please.

I need jquery to display 5

<ul id="numeric" class="sortable boxier" style="margin-right: 1em;">
<li>Test7</li>
<li>Test2</li>
<li>Test6</li>
<li>Test5</li>
<li>Test3</li>
<li>Test8</li>
</ul>

Thank you

+4  A: 
$('li:contains(Test3)').index()

Note that this uses 0-based indexing, so it will actually display 4. You can add 1 if you want to start your count at 1.

lonesomeday
you should also confine it inside the ul, so better use `$('#numeric li:contains(Test3)').index()`, otherwise it would index it in regard to all `li` elements in the page..
Gaby
@Gaby [API](http://api.jquery.com/index/) "If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements." That said, the reference to the parent element is still good practice from a performance perspective.
lonesomeday
@lonesomeday, you are right ! something new learned :)
Gaby