Permalink
Browse files

Added .replaceWith()

  • Loading branch information...
1 parent f6f938e commit 05bfe4d2d1d13f0eac190d0e5d40622835f178af errorhandler committed May 2, 2011
Showing with 20 additions and 0 deletions.
  1. +8 −0 src/zepto.js
  2. +12 −0 test/zepto.html
View
@@ -162,6 +162,14 @@ var Zepto = (function() {
}
})
},
+ replaceWith: function(newContent) {
+ return this.each(function() {
+ var element = $(this),
+ prev = element.prev();
+ element.remove();
+ prev.after(newContent);
+ });
+ },
hide: function(){
return this.css("display", "none")
},
View
@@ -35,6 +35,11 @@
<div id="attr_1" data-id="someId1" data-name="someName1"></div>
<div id="attr_2" data-id="someId2" data-name="someName2"></div>
+ <div class="replacewith">
+ <div class="inner first">Hello</div>
+ <div class="inner second">And</div>
+ <div class="inner third">Goodbye</div>
+ </div>
<div id="data_attr" data-blah="whatever"></div>
@@ -507,6 +512,13 @@
t.assertEqual("1",$("span.c",el).text());
},
+ testReplaceWith: function(t) {
+ $('div.second').replaceWith('<h2 id="replace_test">New heading</h2>');
+ t.assertUndefined($('div.second').get(0));
+ t.assert(document.getElementById("replace_test").nodeType);
+ t.assertEqual($('.replacewith h2#replace_test').get(0), document.getElementById("replace_test"));
+ },
+
testFind: function(t){
var found = $('p#find1').find('span.findme');
t.assertLength(4, found);

0 comments on commit 05bfe4d

Please sign in to comment.