Skip to main content

Bug Tracker

Side navigation

#3971 closed enhancement (fixed)

Opened January 23, 2009 03:43AM UTC

Closed May 02, 2009 07:24PM UTC

Last modified May 02, 2009 07:34PM UTC

.index() enhancement

Reported by: ajpiano Owned by: brandon
Priority: major Milestone: 1.4
Component: core Version: 1.3.1
Keywords: Cc:
Blocked by: Blocking:
Description

It would be useful to be able to use the .index() method to find the index of the current element in a given set, rather than the current implementation (searching the current set for a given element), which forces the developer to do gyrations in order to find out a simple piece of info.

jQuery.fn.index = function(elem){

  // legacy implementation
  if ( typeof elem === 'object'){
    return jQuery.inArray(
              elem && elem.jquery ? elem[0] : elem
             , this );
  }
  //return the index of the element in a new jQuery obj from selector, or by default, amongst its own siblings.
  return jQuery.inArray(this[0],
            elem ? jQuery(elem) : this.parent().children() );

}; 

as per: http://groups.google.com/group/jquery-dev/browse_thread/thread/f5e07e2f81011d8b

Attachments (2)
Change History (5)

Changed March 05, 2009 09:02PM UTC by paul.irish comment:1

Just added a proper patch. Comes with four unit tests, as well.

One thing I recognized while cleaning this up is that in this case, passing a selector into index() vs passing an jquery object of those same elements will give different results.

Because index() can't determine if you're passing a jquery object as an argument because you'd want to see the relative index, or you want to use the legacy implementation of elems.index($(this))

This is a weird break in the expected API, so it may be worth either:

a) removing the $(this).index(selector) functionality

b) renaming this enhanced method .indexWithin() or something.

I still feel strongly that providing the $(this).index() technique is a valuable (and very jquery-like) addition.

Curious what others think about this.

Changed May 02, 2009 03:27PM UTC by brandon comment:2

description: It would be useful to be able to use the .index() method to find the index of the current element in a given set, rather than the current implementation (searching the current set for a given element), which forces the developer to do gyrations in order to find out a simple piece of info. \ \ jQuery.fn.index = function(elem){ \ \ // legacy implementation \ if ( typeof elem === 'object'){ \ return jQuery.inArray( \ elem && elem.jquery ? elem[0] : elem \ , this ); \ } \ //return the index of the element in a new jQuery obj from selector, or by default, amongst its own siblings. \ return jQuery.inArray(this[0], \ elem ? jQuery(elem) : this.parent().children() ); \ \ }; \ \ as per: http://groups.google.com/group/jquery-dev/browse_thread/thread/f5e07e2f81011d8bIt would be useful to be able to use the .index() method to find the index of the current element in a given set, rather than the current implementation (searching the current set for a given element), which forces the developer to do gyrations in order to find out a simple piece of info. \ \ {{{ \ jQuery.fn.index = function(elem){ \ \ // legacy implementation \ if ( typeof elem === 'object'){ \ return jQuery.inArray( \ elem && elem.jquery ? elem[0] : elem \ , this ); \ } \ //return the index of the element in a new jQuery obj from selector, or by default, amongst its own siblings. \ return jQuery.inArray(this[0], \ elem ? jQuery(elem) : this.parent().children() ); \ \ }; \ }}} \ as per: http://groups.google.com/group/jquery-dev/browse_thread/thread/f5e07e2f81011d8b

Changed May 02, 2009 06:16PM UTC by brandon comment:3

owner: → brandon

Changed May 02, 2009 07:24PM UTC by brandon comment:4

milestone: 1.3.21.3.3
resolution: → fixed
status: newclosed

While I can understand the potential confusion about passing a jQuery collection vs a selector. This is something that can be cleared up with proper documentation.

Added in r6330.

Changed May 02, 2009 07:34PM UTC by ajpiano comment:5

woo, thanks for taking care of this.