<p id="myText">Some text</p>CharacterData: replaceWith() method / notebook
CharacterData: replaceWith() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const text = document.getElementById("myText").firstChild;
const em = document.createElement("em");
em.textContent = "Italic text";
text.replaceWith(em); // Replace `Some text` by `Italic text`