.replaceWith()
void Element.replaceWith((Node or String)... nodes)
Warning! Current browsers do not yet support
.replaceWith()
, please
use a polyfill
until they do.
Replaces a node with one or more nodes.
node.replaceWith(nodes);
Replaces node with nodes, while replacing strings in nodes with equivalent text nodes.
Example
HTML:
<p>
All
<strong>large</strong>
goats
<em>are</em>
delicious!
</p>
JavaScript:
let el = document.createElement("strong");
el.textContent = "small";
document.query("strong").replaceWith(el, " baby");
Resulting HTML:
<p>
All
<strong>small</strong>
baby
goats
<em>are</em>
delicious!
</p>