Element.replaceWith() is an instance method from the ChildNode API. It replaces the element itself in its parent’s child list with new nodes or strings. Learn the MDN p → span swap, how it differs from replaceChildren(), and five try-it labs.
01
Kind
Instance method
02
Action
Replace self
03
Args
Nodes or strings
04
Returns
undefined
05
vs children
not inner kids
06
Status
Baseline widely
Fundamentals
Introduction
Older DOM code often used parent.replaceChild(newNode, oldNode) to swap one element for another. replaceWith() expresses the same idea on the element being removed: call it on the node you want gone, and pass what should take its place.
You can pass elements, other nodes, or plain strings. Strings become Text nodes automatically. After the call, the original element is no longer in the document.
💡
Beginner tip
replaceWith() replaces this element in the parent. replaceChildren() replaces the children inside an element—a different target.
param1, …, paramN — a set of Node objects or strings to replace this element with (MDN).
Return value
None (undefined) (MDN).
Exceptions
Thrown when the node cannot be inserted at the specified point in the hierarchy (MDN).
Common patterns
JavaScript
const p = document.querySelector("p");
const span = document.createElement("span");
p.replaceWith(span); // swap element
p.replaceWith("Upgraded"); // swap for text
p.replaceWith(span, " · "); // several replacements in order
Cheat Sheet
⚡ Quick Reference
Goal
Code
Replace element
el.replaceWith(newEl)
Replace with text
el.replaceWith("Hello")
Replace with several
el.replaceWith(a, b)
Classic equivalent
parent.replaceChild(newNode, el)
Return value
undefined
MDN status
Baseline Widely available (since April 2018)
Snapshot
🔍 At a Glance
Four facts to remember about Element.replaceWith().
Returns
undefined
Original el removed
Baseline
widely
Since April 2018
Target
self
Not children
Strings
→ Text
Auto Text nodes
Compare
📋 replaceWith() vs replaceChildren() vs remove()
replaceWith()
replaceChildren()
remove()
What changes
The element itself
Children inside element
Element removed only
Inserts replacement
Yes (args)
Yes (args)
No
Strings
Yes (as Text)
Yes (as Text)
N/A
Return value
undefined
undefined
undefined
Best for
Swap this element
Swap inner content
Delete with no replacement
Hands-On
Examples Gallery
Examples follow MDN Element.replaceWith() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
MDN’s p → span swap and text replacement.
Example 1 — Replace With an Element (MDN)
Swap a <p> for a <span> inside a div.
JavaScript
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() is Baseline Widely available (MDN: across browsers since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.replaceWith()
Safe for production. Replace an element with nodes or strings using a modern ChildNode API.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerNot supported · Use replaceChild polyfill pattern
No
replaceWith()Excellent
Bottom line: Prefer replaceWith() when you think “swap this element for something else.” Use replaceChildren() to change inner content only. Use remove() when you need no replacement. Strings become Text nodes automatically.
Wrap Up
Conclusion
Element.replaceWith() is the modern way to swap an element for new nodes or text—one or several at once. It returns undefined and removes the original element from the DOM.
Build the replacement node before calling replaceWith
Verify with outerHTML or querySelector
❌ Don’t
Expect a returned node (it is undefined)
Confuse replacing self with replacing children
Assume IE supports replaceWith() without a fallback
Forget that invalid trees can throw HierarchyRequestError
Hold stale references to the removed element after the swap
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.replaceWith()
Swap an element for nodes or text—one call, same sibling position.
5
Core concepts
📝01
Returns
undefined
API
📄02
Target
this element
Self
✍️03
Strings
become Text
Coerce
✅04
Baseline
widely available
Status
⚡05
Classic
replaceChild
DOM
❓ Frequently Asked Questions
It replaces this element in its parent's children list with the specified Node objects or strings (MDN). Strings become Text nodes.
No. MDN marks Element.replaceWith() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
Nothing useful — the return value is undefined (MDN). The original element is removed from the DOM.
replaceWith() replaces the element itself. replaceChildren() replaces only the children inside an element.
Yes. replaceWith(node1, node2, …) inserts all arguments in place of the element you call it on (MDN).
Yes. MDN: replaceWith() is not available inside a with statement — use el.replaceWith(...) instead.
Did you know?
replaceWith() comes from the ChildNode mixin—the same family as before(), after(), and remove(). MDN also notes it is unscopable, so it is not available inside a with statement.