jQuery - Get index value of selected LI

[COLOR=#888888][B][COLOR=#000000]In the code below I’m trying to find the index value of the LI where the link is selected?

[/COLOR] [/B][/COLOR]


<ul id="myList">
<li><a href="#">apples</a></li>
<li><a class="linkSelected" href="#">oranges</a></li>
<li><a href="#">pears</a></li>
<li><a href="#">bannas</a></li>
<ul>

[COLOR=#888888]

var listArray = $(‘#myList li’);
[COLOR=#000000]var selectedLI = listArray.find(‘.linkSelected’).parent().index();

that isn’t quite right?
I need the index value of oranges (1)

Help

[/COLOR][/COLOR]

You are using the index() function the wrong way (see the documentation).
So in your case you would need to do:


var listArray = $('#myList li');
var selectedLI = listArray.index( listArray.find('.linkSelected').parent() );

In the case of the oranges the index value would now indeed be 1, since it is 0-based.

PS. Technically speaking, listArray is not an Array, but an Object.


var listArray = $('#myList li');
var selectedLI = listArray.index( listArray.find('.linkSelected').parent() );

works well, but what is selectedLI? It must be a jQuery object because it steps into my if statement below even when it equals zero??

if (selectedLI != 0) {

}

oh, nevermind, it just looked like it stepped into it in firebug, but it wasn’t. My bad.