Might want to be careful with that last suggestion, and be sure to specify the 
specific <ul> in question in case there are other <li> on the page.

i.e. $("ul#theid li")

though i am just learning jquery and am not sure if that is the correct syntax 
to select the <li> in a particular <ul> by id.

----- Original Message ----
From: John Resig <[EMAIL PROTECTED]>
To: jQuery Discussion <discuss@jquery.com>
Sent: Friday, March 23, 2007 12:40:52 PM
Subject: Re: [jQuery] Best way to find current position in DOM stack?

You want the index() function:

var item = $("li.selected")[0];
var position = $("li").index( item );

On 3/23/07, Nate Cavanaugh <[EMAIL PROTECTED]> wrote:
>
> Hi all,
> I'm looking to find the current numeric position of a certain element
> amongst it's siblings.
>
> Let's say I have a list like so:
>
> <ul>
> <li>Test 1</li>
> <li>Test 2</li>
> <li class="selected">Test 3</li>
> <li>Test 4</li>
> <li>Test 5</li>
> </ul>
>
> So, I grab $('li.selected'). I now want to find out what number it is (in
> this case, I would want it to return 3).
>
> Currently, I am doing it this way, but I am hoping for a more jQuery-esque
> way:
>
> var item = $('li.selected')[0];
> var position = 1;
> $('li').each(function() {
> if(this != item){
> position++;
> }else {
> return false;
> }
> });
>
> Seems a bit ugly, but it could be the only way, I dunno.
>
> Anyone have any thoughts?
> --
> View this message in context: 
> http://www.nabble.com/Best-way-to-find-current-position-in-DOM-stack--tf3455334.html#a9639907
> Sent from the JQuery mailing list archive at Nabble.com.
>
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/




_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to