index now works in reverse when passed a selector or undefined. fixes #3971

This commit is contained in:
Brandon Aaron 2009-05-02 19:22:55 +00:00
parent cbda6c541b
commit ffd457d456
2 changed files with 40 additions and 24 deletions

View file

@ -182,10 +182,16 @@ jQuery.fn = jQuery.prototype = {
// Determine the position of an element within // Determine the position of an element within
// the matched set of elements // the matched set of elements
index: function( elem ) { index: function( elem ) {
if ( !elem || typeof elem === "string" ) {
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() );
}
// Locate the position of the desired element // Locate the position of the desired element
return jQuery.inArray( return jQuery.inArray(
// If it receives a jQuery object, the first element is used // If it receives a jQuery object, the first element is used
elem && elem.jquery ? elem[0] : elem, this ); elem.jquery ? elem[0] : elem, this );
}, },
is: function( selector ) { is: function( selector ) {

View file

@ -366,12 +366,13 @@ test("each(Function)", function() {
ok( pass, "Execute a function, Relative" ); ok( pass, "Execute a function, Relative" );
}); });
test("index(Object)", function() { test("index(Object|String|undefined)", function() {
expect(10); expect(15);
var elements = jQuery([window, document]), var elements = jQuery([window, document]),
inputElements = jQuery('#radio1,#radio2,#check1,#check2'); inputElements = jQuery('#radio1,#radio2,#check1,#check2');
// Passing a node
equals( elements.index(window), 0, "Check for index of elements" ); equals( elements.index(window), 0, "Check for index of elements" );
equals( elements.index(document), 1, "Check for index of elements" ); equals( elements.index(document), 1, "Check for index of elements" );
equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" ); equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
@ -381,9 +382,18 @@ test("index(Object)", function() {
equals( inputElements.index(window), -1, "Check for not found index" ); equals( inputElements.index(window), -1, "Check for not found index" );
equals( inputElements.index(document), -1, "Check for not found index" ); equals( inputElements.index(document), -1, "Check for not found index" );
// Passing a jQuery object
// enabled since [5500] // enabled since [5500]
equals( elements.index( elements ), 0, "Pass in a jQuery object" ); equals( elements.index( elements ), 0, "Pass in a jQuery object" );
equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" ); equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
equals( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
// Passing a selector or nothing
// enabled since [6329]
equals( jQuery('#text2').index(), 2, "Check for index amongst siblings" );
equals( jQuery('#form').children().eq(4).index(), 4, "Check for index amongst siblings" );
equals( jQuery('#radio2').index('#form :radio') , 1, "Check for index within a selector" );
equals( jQuery('#radio2').index('#form :text') , -1, "Check for index not found within a selector" );
}); });
test("jQuery.merge()", function() { test("jQuery.merge()", function() {