MDN self-hosted library · e01.org

Element: replaceWith() method

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
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>"
js
with (node) {
  replaceWith("foo");
}
// ReferenceError: replaceWith is not defined