Element.replaceWith() replaces this element in its parent’s children list with one or more Node objects or strings (MDN). Strings become Text nodes. Returns undefined.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.replaceWith. Start with this line.
JavaScript
el.replaceWith(…)
Call
What it does
el.replaceWith(…)
Primary signature.
el.replaceWith(...)
Call with the arguments your case needs.
Built into the browser. Call replaceWith(...) on an Element — no import required.
Library declarations
JavaScript
el.replaceWith(…)
Parameters
Parameter
Description
param1…paramN
Required. Node objects and/or strings
Return Value
Return Value
Description
*
Always undefined
JavaScript Examples
Example 1 — Replace with an Element (MDN)
Swap a p for a span inside the same parent.
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>"
One call replaces “one node → many” without extra inserts.
Compatibility
Browser Support
Element.replaceWith() is Baseline Widely available on MDN (across browsers since April 2018). Safe for everyday DOM swaps.
✓ Baseline Widely available
Element.replaceWith()
Replace this element with nodes or text — widely supported.
WideBaseline
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
replaceWith()Widely available
Bottom line: Call el.replaceWith(…) to swap the element itself. Use replaceChildren() when you only need to change its kids.
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.
undefined. There is no return value (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.