const div = document.createElement("div");
const p = document.createElement("p");
div.appendChild(p);
const span = document.createElement("span");
p.replaceWith(span);
console.log(div.outerHTML);
// "<div><span></span></div>"Element: replaceWith() method / notebook
Element: replaceWith() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
with (node) {
replaceWith("foo");
}
// ReferenceError: replaceWith is not defined