diff --git a/src/manipulation.js b/src/manipulation.js index 3a4f9a688..196794f83 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -160,10 +160,12 @@ jQuery.fn.extend({ // keepData is for internal use only--do not document remove: function( selector, keepData ) { var elem, - i = 0; + elems = selector ? jQuery.filter( selector, this ) : this, + i = elems.length; + + while ( i-- ) { + elem = elems[ i ]; - for ( ; (elem = this[i]) != null; i++ ) { - if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) { if ( !keepData && elem.nodeType === 1 ) { jQuery.cleanData( getAll( elem ) ); } @@ -175,7 +177,6 @@ jQuery.fn.extend({ elem.parentNode.removeChild( elem ); } } - } return this; }, diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index f06e51098..c3c83b4f5 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1741,7 +1741,8 @@ test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function() }); var testRemove = function( method ) { - var first = jQuery("#ap").children().first(); + var markup, div, + first = jQuery("#ap").children().first(); first.data("foo", "bar"); @@ -1759,6 +1760,18 @@ var testRemove = function( method ) { jQuery("#ap").children()[ method ]("a, code"); equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" ); + // Positional and relative selectors + markup = "