Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:23:53+00:00 2026-05-27T11:23:53+00:00

HTML: <ul> <li class=selected>First Item</li> <li class=disabled>Second Item</li> <li class=separator>Third Item</li> <li>Fourth Item</li> </ul>

  • 0

HTML:

<ul>
    <li class="selected">First Item</li>
    <li class="disabled">Second Item</li>
    <li class="separator">Third Item</li>
    <li>Fourth Item</li>
</ul>

jQuery:

alert($("li:not(.disabled,.separator)").index());

According to the documentation for index:

the return value is an integer indicating the position of the first
element within the jQuery object relative to its sibling elements.

Emphasis on first element. But the above code returns 3. Based on the documentation shouldn’t this code return 0?

You can see it in action here: http://jsfiddle.net/Zf9Vv/

NOTE:

My selector matches two elements: the first and last LI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:23:54+00:00Added an answer on May 27, 2026 at 11:23 am

    Edit (see comments)
    The original answer is incorrect… I’m going to keep it here for now so the comments make sense.

    Looking at the jQuery source for index, you can see the following snippet:

    if ( !elem ) {
        return ( this[0] && this[0].parentNode ) ? this.prevAll().length : -1;
    }
    

    Compare this to the corresponding (if very different) snippet from an earlier release, 1.6.2. Note the use of this[0]:

    return jQuery.inArray( this[0],
    // If it receives a string, the selector is used
    // If it receives nothing, the siblings are used
    elem ? jQuery( elem ) : this.parent().children() );
    

    It seems that in the current version the this.prevAll part causes the problem. If you change it to this.eq(0).prevAll (which replicates what the index documentation states) then you get the correct value returned. So it would appear this is a jQuery bug.

    In the 1.6.2 version, inArray is used. That method returns the index of the first argument in the second argument (or -1 if the first argument is not found in the second). As the first argument is this[0] (the first element in the matched set) we get the expected result.

    Here’s an updated fiddle with the modified jQuery source included. The correct result is alerted.


    Original answer (this is incorrect):

    Read the quoted part of the docs again carefully (bold highlighting added):

    the return value is an integer indicating the position of the first
    element within the jQuery object relative to its sibling elements.

    Just because two of the siblings have been removed from the matched set, it doesn’t change the value returned by index. In other words, the matched element (<li>Fourth Item</li>) will always have index 3, relative to its siblings (unless, of course, new siblings are inserted into the DOM before the element in question).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such html and css. <div class=selected> <div class=text>First</div> <div class=arrow>&nbsp;</div> </div> .selected
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have this piece of code: <li class=selected><a href=/AmIACandidate.html>Am I a Candidate?</a></li> I need
Is there any html form class(text box) that does not allow null values. The
How can I use the selected item from a jQuery-ui autocomplete in a further
I'm working on an HTML class in PHP, so that we can keep all
I know in the MVC Framework, you have the Html Class to create URLs:
I want to use the Html Helper class to build, for example, an Html.ActionLink
is it possible to generate cross-application action links using the HTML helper class? I
As detailed here among other places, the only valid characters in a html/css class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.