Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #154 from errorhandler/master.

Added .replaceWith
  • Loading branch information...
commit eb4f84ade7fcd5a7cfc962cef5f9700b0b16076b 2 parents f6f938e + 7b0e91e
@madrobby authored
Showing with 28 additions and 0 deletions.
  1. +8 −0 src/zepto.js
  2. +20 −0 test/zepto.html
View
8 src/zepto.js
@@ -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
20 test/zepto.html
@@ -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>
@@ -138,6 +143,8 @@
<input type="text" id="BooleanInput" required />
<form id="some_form"></form>
+
+ <div class="replace_test_div">test</div>
<script>
@@ -507,6 +514,19 @@
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"));
+
+ $('#replace_test').replaceWith($('#replace_test_div'));
+ t.assertUndefined($('#replace_test').get(0));
+ t.assert(document.getElementsByClassName("replace_test_div")[0].nodeType);
+ t.assertEqual($('.replacewith h2#replace_test').get(0), document.getElementsByClassName("replace_test")[0]);
+ },
+
testFind: function(t){
var found = $('p#find1').find('span.findme');
t.assertLength(4, found);
Please sign in to comment.
Something went wrong with that request. Please try again.