Node.replaceChild(newChild, oldChild) replaces one child of a parent with another node and returns the replaced node (same as oldChild) (MDN).
Baseline · Widely available: not Deprecated, Experimental, or Non-standard (since July 2015).
Warning: if newChild is already in the DOM, it is moved (removed from its old place first), not cloned (MDN).
Note: argument order is unusual — newChild first, oldChild second. Element.replaceWith() often reads more clearly for elements (MDN).
Remember
parent.replaceChild(newChild, oldChild) → oldChild
order: NEW first, OLD second
newChild already in DOM → moved, not cloned
MDN pattern:
parent.replaceChild(sp1, sp2)
Call it on the parent: new node first, old child second. Keep the return value if you need the old node.
Get the parent and the oldChild that belongs to it.
Create or locate newChild.
Call parent.replaceChild(newChild, oldChild).
Store the return value if you want to reuse the old node.
Piece
Required?
Role
Receiver parent
Yes
Current parent of oldChild
newChild
Yes
Node that takes oldChild’s place
oldChild
Yes
Child to replace (must belong to parent)
Return value
—
The same oldChild node
Pseudocode
Pseudocode
if oldChild.parent is not this node:
throw NotFoundError
if replacement would break DOM rules:
throw HierarchyRequestError
if newChild is already in the tree:
detach newChild from its current parent
put newChild where oldChild was
detach oldChild
return oldChild
Reference
Cheat Sheet
Keep these facts handy when swapping children in the tree.
Topic
Detail
Syntax
parent.replaceChild(newChild, oldChild)
Arg order
New first, old second (unusual)
Returns
The replaced oldChild
Already in DOM?
newChild is moved, not cloned
NotFoundError
oldChild is not a child of that parent
HierarchyRequestError
Invalid tree (cycle, wrong types, bad document shape)
Modern alternative
oldChild.replaceWith(newChild)
Status (MDN)
Baseline Widely available
Try it
Live Preview
Swap a child, check argument order, move a node from another parent, or read the returned old node.
Browser demo of Node.replaceChild() — swap, move, or reuse the returned node.
Result
Loading…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Basic Swap (MDN)
Replace an existing span with a new span under the same parent.
Prefer a single replaceChild for one swap. For wiping and rebuilding a container, replaceChildren() is often clearer.
Remember
Key Takeaways
Role: put newChild where oldChild was and return the old node.
Order: new first, old second — easy to reverse by mistake.
Move: an in-tree newChild is relocated, not cloned.
Errors: wrong parent → NotFoundError; illegal tree → HierarchyRequestError.
One line:parent.replaceChild(newChild, oldChild) swaps one child and hands back the old node.
Frequently Asked Questions
It replaces one child of a parent with another node. Call it on the parent: replaceChild(newChild, oldChild). It returns the replaced node (the same as oldChild).
No. MDN marks Node.replaceChild() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
MDN notes the order is unusual: newChild first, oldChild second. Remember “put the new one in, take the old one out.” Element.replaceWith() can be easier to read for elements.
MDN warns that if the new node is already present somewhere else in the DOM, it is first removed from that position, then inserted in place of oldChild.
NotFoundError if oldChild is not a child of the parent you called. HierarchyRequestError when the replacement would violate DOM tree rules (cycles, wrong node types, invalid document structure).
If you already have the old element and only need to swap it, oldChild.replaceWith(newChild) reads more naturally. Use replaceChild when you work from the parent or need classic Node API patterns.