Replacing DOM Elements - J Query

Problem
You need to replace DOM nodes currently in the DOM with new DOM nodes.

Solution

Using the replace With() method, we can select a set of DOM elements for replacement .In the following code example, we use the replace With() method to replace all <li> elements with a class attribute of remove with a new DOM structure:

The new DOM structure added to the DOM is a string parameter passed into the replaceWith() method. In our example, all the <li>elements, including children elements, are replaced with the new structure, <li>removed</li>

Discussion
jQuery provides an inverse to this method called replaceAll() that does the same task with the parameters reversed. For example, we could rewrite the jQuery code found inour recipe code like so:

Here we are passing the jQuery function the HTML string and then using the replaceAll() method to select the DOM node and its children that we want to be removed and replaced.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics