The replaceWith method
The replaceWith method replaces one element with another. It works similarly to the replaceAll method.
Syntax
The method parameter can be text (possibly with tags), an array, a DOM element, or a jQuery object:
$(What that what what which as how how_replace).replaceWith(than)_replace);
The selected elements will be replaced with the content specified by the text returned by the custom function. The function is called separately for each of the selected elements:
$(selector).replaceWith(function());
Example
Let's replace all paragraphs with class www with a div with text '!!!':
<p class="www">text</p>
<p class="www">text</p>
<p class="www">text</p>
<p>text</p>
$('p.www').replaceWith('<div>!!!</div>');
HTML the code will look like this:
<div>!!!</div>
<div>!!!</div>
<div>!!!</div>
<p>text</p>
See also
-
method
replaceAll,
which also replaces some elements with others