Skip to main content

Bug Tracker

Side navigation

#13347 closed bug (notabug)

Opened January 29, 2013 04:28PM UTC

Closed January 29, 2013 05:59PM UTC

Last modified January 29, 2013 07:17PM UTC

index( undefined ) returning incorrect values

Reported by: Motty Owned by:
Priority: undecided Milestone: None
Component: traversing Version:
Keywords: Cc:
Blocked by: Blocking:
Description

The index of an undefined element should return -1, but instead returns an incorrect value (http://jsfiddle.net/Mottie/FMh69/)


// jQuery ver = value returned
// 1.6.4   = 2
// 1.7.2   = 2
// 1.8.3   = 2
// 1.9.0   = 0
// 2.0.0b1 = 0
var undef;
console.log( $('div').index( undef ) );
Attachments (0)
Change History (3)

Changed January 29, 2013 05:41PM UTC by dmethvin comment:1

_comment0: I feel it's important to point out that an undefined value is not one of the four things that `.index()` is documented to take: no arguments, a selector string, a DOM element, or a jQuery collection. So there is no "should" here, although we might decide to define new behavior. It looks like up until 1.9 it was being interpreted as the "no arguments" case. \ \ 1359482018864128

I feel it's important to point out that an undefined value is not one of the four things that .index() is documented to take: no arguments, a selector string, a DOM element, or a jQuery collection. So there is no "should" here, although we might decide to define new behavior. It looks like up until 1.9 it was being interpreted as the "no arguments" case. -- Correction, it *is* the no-args case but there was a bug before 1.9.

Changed January 29, 2013 05:59PM UTC by gibson042 comment:2

component: unfiledtraversing
resolution: → notabug
status: newclosed

The behavior change you observe stems from fixing #10977, but the invalid input undefined was treated like "no argument" (returns position in parent of the collection's first member) even before then. You probably want to do something like element ? $collection.index( element ) : -1.

Changed January 29, 2013 07:17PM UTC by Motty comment:3

I was just troubleshooting some code that was passing an undefined variable to the index. I'll just make sure to follow gibson042's recommendation from now on, thanks! :)