files/en-us/web/api/characterdata/replacewith/index.md
{{APIRef("DOM")}}
The replaceWith() method of the {{domxref("CharacterData")}} interface
replaces this node in the children list of its parent
with a set of {{domxref("Node")}} objects or string.
Strings are inserted as {{domxref("Text")}} nodes; the string is being passed as argument to the {{domxref("Text/Text", "Text()")}} constructor.
replaceWith(...nodes)
nodes {{optional_inline}}
[!NOTE] If no arguments are passed in, this method removes the node from the DOM tree.
None ({{jsxref("undefined")}}).
HierarchyRequestError {{DOMxRef("DOMException")}}
<p id="myText">Some text</p>
let text = document.getElementById("myText").firstChild;
let em = document.createElement("em");
em.textContent = "Italic text";
text.replaceWith(em); // Replace `Some text` by `Italic text`
{{EmbedLiveSample("Examples", "100%", 30)}}
{{Specifications}}
{{Compat}}