Friday, December 6, 2013

jQuery - replace html element by .remove()

.remove()
Remove the set of matched elements from the DOM.

You could remove an element from DOM but it doesn't get eliminated from wrapper set and this feature allows you remove an element, operate on that element, and then place that element back into the DOM, all within a single jQuery chain.

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<title></title> </head> <body> <div id="ItemToBeRelaced">remove it!</div>
<script> (function ($) { $('#ItemToBeRelaced') .remove() .html('<a href="http://greyknots.com/">GreyKnots</a>') .appendTo('body'); })(jQuery); </script>
</body> </html>

Share/Bookmark

No comments: