Skip to main content

Bug Tracker

Side navigation

#10977 closed bug (fixed)

Opened December 08, 2011 12:35AM UTC

Closed October 16, 2012 02:25PM UTC

Incorrect documentation for the index method?

Reported by: AbeMiessler@yahoo.com Owned by: rwaldron
Priority: high Milestone: None
Component: traversing Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description

You have the following documentation for index with no parameters:

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.

But if you run this jQuery:

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

Against this 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>

It returns 3 instead of 0. You can see that the selector will find two elements (the first and last LI), but it returns the index of the last element, when your documentation says it will return the index of the first.

Can be seen in this fiddle: http://jsfiddle.net/Zf9Vv/

Attachments (0)
Change History (8)

Changed December 08, 2011 12:56AM UTC by john.papaioannou@gmail.com comment:1

I have tested this snippet in multiple jQuery versions, and discovered that it is a regression introduced in 1.6.3. Please see this question on StackOverflow (and specifically, this answer) that pinpoints the guilty code.

Changed December 08, 2011 01:26AM UTC by anonymous comment:2

Culprit: https://github.com/jquery/jquery/commit/9a96af1d59cc6a10b89b6f2489b04871a9c1c7fa#src/traversing.js

Fix, add first()

return ( this[0] && this[0].parentNode ) ? this.first().prevAll().length : -1;

Changed December 08, 2011 01:33AM UTC by rwaldron comment:3

component: unfiledtraversing
owner: → rwaldron
priority: undecidedhigh
status: newassigned

Changed December 08, 2011 10:37AM UTC by addyosmani comment:4

keywords: → needsdocs

Changed September 17, 2012 04:12PM UTC by mikesherov comment:5

keywords: needsdocs
resolution: → notabug
status: assignedclosed

Changed October 14, 2012 07:27PM UTC by mikesherov comment:6

resolution: notabug
status: closedreopened

Changed October 14, 2012 08:45PM UTC by mikesherov comment:7

status: reopenedopen

Changed October 16, 2012 02:25PM UTC by MORGAN comment:8

resolution: → fixed
status: openclosed

Return correct index for no-arg index() calls. Fixes #10977. Closes gh-971

Changeset: 4bb46f413a0e2f6933013b26d2aceddca6cf03f1