Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added new replaceWith() (replace all matched elements with the specif…
…ied HTML/DOM Elements/Array/etc.) and replaceAll() (replace the specified elements with the set of matched elements).
  • Loading branch information
jeresig committed Aug 25, 2007
1 parent 0477a6e commit 3ae74b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/jquery/coreTest.js
Expand Up @@ -604,6 +604,54 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
ok( expected == $('#en').text(), "Insert jQuery after" );
});

test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
expect(10);
$('#yahoo').replaceWith('<b id="replace">buga</b>');
ok( $("#replace")[0], 'Replace element with string' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );

reset();
$('#yahoo').replaceWith(document.getElementById('first'));
ok( $("#first")[0], 'Replace element with element' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );

reset();
$('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
ok( $("#first")[0], 'Replace element with array of elements' );
ok( $("#mark")[0], 'Replace element with array of elements' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );

reset();
$('#yahoo').replaceWith($("#first, #mark"));
ok( $("#first")[0], 'Replace element with set of elements' );
ok( $("#mark")[0], 'Replace element with set of elements' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
});

test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(10);
$('<b id="replace">buga</b>').replaceAll("#yahoo");
ok( $("#replace")[0], 'Replace element with string' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );

reset();
$(document.getElementById('first')).replaceAll("#yahoo");
ok( $("#first")[0], 'Replace element with element' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );

reset();
$([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
ok( $("#first")[0], 'Replace element with array of elements' );
ok( $("#mark")[0], 'Replace element with array of elements' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );

reset();
$("#first, #mark").replaceAll("#yahoo");
ok( $("#first")[0], 'Replace element with set of elements' );
ok( $("#mark")[0], 'Replace element with set of elements' );
ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
});

test("end()", function() {
expect(3);
ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
Expand Down
7 changes: 6 additions & 1 deletion src/jquery/jquery.js
Expand Up @@ -1182,6 +1182,10 @@ jQuery.fn = jQuery.prototype = {
this.empty().append( val );
},

replaceWith: function( val ) {
return this.after( val ).remove();
},

slice: function() {
return this.pushStack( Array.prototype.slice.apply( this, arguments ) );
},
Expand Down Expand Up @@ -2227,7 +2231,8 @@ jQuery.each({
appendTo: "append",
prependTo: "prepend",
insertBefore: "before",
insertAfter: "after"
insertAfter: "after",
replaceAll: "replaceWith"
}, function(i,n){
jQuery.fn[ i ] = function(){
var a = arguments;
Expand Down

0 comments on commit 3ae74b5

Please sign in to comment.