aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2007-08-25 05:12:20 +0000
committerJohn Resig <jeresig@gmail.com>2007-08-25 05:12:20 +0000
commit3ae74b523ec379a1753d116bb7f1aec8db2c52d4 (patch)
tree883e5ae3ec74b3bf68233eef87ce2ef70d4ff171 /src
parent0477a6e99e95ae93da983e7fc2a30bf16ea8ca77 (diff)
downloadjquery-3ae74b523ec379a1753d116bb7f1aec8db2c52d4.tar.gz
jquery-3ae74b523ec379a1753d116bb7f1aec8db2c52d4.zip
Added new replaceWith() (replace all matched elements with the specified HTML/DOM Elements/Array/etc.) and replaceAll() (replace the specified elements with the set of matched elements).
Diffstat (limited to 'src')
-rw-r--r--src/jquery/coreTest.js48
-rw-r--r--src/jquery/jquery.js7
2 files changed, 54 insertions, 1 deletions
diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js
index 312bc3ce1..e91928497 100644
--- a/src/jquery/coreTest.js
+++ b/src/jquery/coreTest.js
@@ -604,6 +604,54 @@ test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
ok( expected == $('#en').text(), "Insert jQuery after" );
});
+test("replaceWith(String|Element|Array&lt;Element&gt;|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' );
diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js
index 4c5941b20..8988c5ee1 100644
--- a/src/jquery/jquery.js
+++ b/src/jquery/jquery.js
@@ -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 ) );
},
@@ -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;