Skip to content

Commit 75a973d

Browse files
committed
Made it so that appendTo, etc. return the inserted elements (thus using pushStack, as well). Fixes bugs jquery#3966 and jquery#4182.
1 parent 3e46bce commit 75a973d

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/core.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ jQuery.fn = jQuery.prototype = {
502502
if ( this[0] ) {
503503
var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
504504
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ),
505-
first = fragment.firstChild,
506-
extra = this.length > 1 ? fragment.cloneNode(true) : fragment;
505+
first = fragment.firstChild;
507506

508507
if ( first )
509508
for ( var i = 0, l = this.length; i < l; i++ )
510-
callback.call( root(this[i], first), i > 0 ? extra.cloneNode(true) : fragment );
511-
509+
callback.call( root(this[i], first), this.length > 1 || i > 0 ?
510+
fragment.cloneNode(true) : fragment );
511+
512512
if ( scripts )
513513
jQuery.each( scripts, evalScript );
514514
}
@@ -1189,13 +1189,16 @@ jQuery.each({
11891189
insertAfter: "after",
11901190
replaceAll: "replaceWith"
11911191
}, function(name, original){
1192-
jQuery.fn[ name ] = function() {
1193-
var args = arguments;
1192+
jQuery.fn[ name ] = function( selector ) {
1193+
var ret = [], insert = jQuery( selector );
11941194

1195-
return this.each(function(){
1196-
for ( var i = 0, length = args.length; i < length; i++ )
1197-
jQuery( args[ i ] )[ original ]( this );
1198-
});
1195+
for ( var i = 0, l = insert.length; i < l; i++ ) {
1196+
var elems = (i > 0 ? this.clone(true) : this).get();
1197+
jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
1198+
ret = ret.concat( elems );
1199+
}
1200+
1201+
return this.pushStack( ret, name, selector );
11991202
};
12001203
});
12011204

test/unit/core.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
909909
});
910910

911911
test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
912-
expect(7);
912+
expect(12);
913913
var defaultText = 'Try them out:'
914914
jQuery('<b>buga</b>').appendTo('#first');
915915
equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
@@ -936,6 +936,27 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
936936
reset();
937937
jQuery('#select1').appendTo('#foo');
938938
t( 'Append select', '#foo select', ['select1'] );
939+
940+
reset();
941+
var div = jQuery("<div/>").click(function(){
942+
ok(true, "Running a cloned click.");
943+
});
944+
div.appendTo("#main, #moretests");
945+
946+
jQuery("#main div:last").click();
947+
jQuery("#moretests div:last").click();
948+
949+
reset();
950+
var div = jQuery("<div/>").appendTo("#main, #moretests");
951+
952+
equals( div.length, 2, "appendTo returns the inserted elements" );
953+
954+
div.addClass("test");
955+
956+
ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
957+
ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
958+
959+
reset();
939960
});
940961

941962
test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {

0 commit comments

Comments
 (0)