Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made it so that appendTo, etc. return the inserted elements (thus usi…

…ng pushStack, as well). Fixes bugs #3966 and #4182.
  • Loading branch information...
commit 75a973da35d4ad6583ebef8c389cebbef61bc5b4 1 parent 3e46bce
John Resig jeresig authored

Showing 2 changed files with 35 additions and 11 deletions. Show diff stats Hide diff stats

  1. +13 10 src/core.js
  2. +22 1 test/unit/core.js
23 src/core.js
@@ -502,13 +502,13 @@ jQuery.fn = jQuery.prototype = {
502 502 if ( this[0] ) {
503 503 var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
504 504 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;
507 506
508 507 if ( first )
509 508 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 +
512 512 if ( scripts )
513 513 jQuery.each( scripts, evalScript );
514 514 }
@@ -1189,13 +1189,16 @@ jQuery.each({
1189 1189 insertAfter: "after",
1190 1190 replaceAll: "replaceWith"
1191 1191 }, function(name, original){
1192   - jQuery.fn[ name ] = function() {
1193   - var args = arguments;
  1192 + jQuery.fn[ name ] = function( selector ) {
  1193 + var ret = [], insert = jQuery( selector );
1194 1194
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 );
1199 1202 };
1200 1203 });
1201 1204
23 test/unit/core.js
@@ -909,7 +909,7 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
909 909 });
910 910
911 911 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
912   - expect(7);
  912 + expect(12);
913 913 var defaultText = 'Try them out:'
914 914 jQuery('<b>buga</b>').appendTo('#first');
915 915 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() {
936 936 reset();
937 937 jQuery('#select1').appendTo('#foo');
938 938 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();
939 960 });
940 961
941 962 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {

0 comments on commit 75a973d

Please sign in to comment.
Something went wrong with that request. Please try again.