Skip to main content

Bug Tracker

Side navigation

#2771 closed bug (fixed)

Opened May 02, 2008 07:15AM UTC

Closed May 08, 2008 04:16PM UTC

Last modified October 14, 2008 10:32AM UTC

jQuery.index cannot index the jQuery object

Reported by: ericsk Owned by: flesler
Priority: minor Milestone: 1.2.4
Component: core Version: 1.2.3
Keywords: index Cc:
Blocked by: Blocking:
Description

While using

 jQuery.index 
like this:

  ...
  var target = $(e.target);
  var target_index = $('.item').index(target);
  ...

it always returns -1 (i.e. NOT FOUND).

I suggest to apply the patch in the attachment to

 core.js 
(revision 5375).

Attachments (2)
  • core.js.patch (0.4 KB) - added by ericsk May 02, 2008 07:29AM UTC.

    the patch file of core.js

  • index.diff (0.4 KB) - added by flesler May 06, 2008 07:14PM UTC.

    Passes all the tests, on all browsers.

Change History (6)

Changed May 02, 2008 01:55PM UTC by scott.gonzal comment:1

If this concept is approved, this would probably be better:

elem = elem[0] || elem;

Changed May 02, 2008 02:32PM UTC by flesler comment:2

Disagree...

If you accept collections then they can have more than one element, that means that you get the index of the first item found from the collection.

If this is just a shortcut for $().index( target[0] ); then just do that in your code, not internally.

Changed May 02, 2008 04:51PM UTC by joern comment:3

The shortcut would indeed be helpful as it would improve API consistancy quite a bit. Most other jQuery methods that accept an selector work with a string (css etc. selector) and a single or an array of dom elements. I don't know how many times I tried to use index() expecting that it would use just the first element of my jQuery object, but it happended often. Instead it just returns not-found...

Changed May 06, 2008 07:12PM UTC by flesler comment:4

need: ReviewCommit
owner: → flesler
status: newassigned

I crafted a modified version of .index() that supports collections, even with many elements.

While doing this, I discovered that on IE, window == document, so this could yield some unexpected results when both are in a collection.

I also added a return false, so that when the first index is found, it stops looping.

Here I attach the diff, if you want to discuss/test/benchmark it, be my guest. Let me know when I can commit this.

Changed May 08, 2008 04:16PM UTC by flesler comment:5

resolution: → fixed
status: assignedclosed

Applied at [5500], changed the diff a bit, the === is applied to $.inArray, and this function is used in $.fn.index.

If a jQuery object is received, only the first element is used.

Changed May 08, 2008 05:17PM UTC by flesler comment:6

Added 2 tests at [5505].