/ Published in: JavaScript
Replaces a DOM Element with another, but keeps the classes and IDs of the old one.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
jQuery.fn.replaceWith = function(replacement) { return this.each(function(){ element = $(this); $(this) .after(replacement).next() .attr('class', element.attr('class')).attr('id',element.attr('id')) .html(element.html()) .prev().remove(); }); }; /* usage example $('a#fooid').replaceWith('<span></span>'); before: <a id="fooid" class="whatever">some text</a> after: <span id="fooid" class="whatever">some text</span> */