MDN self-hosted library · e01.org

CharacterData: replaceWith() method

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

html
<p id="myText">Some text</p>
js
const text = document.getElementById("myText").firstChild;
const em = document.createElement("em");
em.textContent = "Italic text";

text.replaceWith(em); // Replace `Some text` by `Italic text`