Name
Node.replaceChild( ): replace a child node with a new node — DOM Level 1 Core
Synopsis
Node replaceChild(NodenewChild, NodeoldChild) throws DOMException;
Arguments
-
newChild The replacement node.
-
oldChild The node to be replaced.
Returns
The node that was removed from the document and replaced.
Throws
This method may throw a DOMException with the following
code values:
-
HIERARCHY_REQUEST_ERR This node does not allow children, it does not allow children of the specified type, or
newChildis an ancestor of this node (or is this node itself).-
WRONG_DOCUMENT_ERR newChildand this node have different values forownerDocument.-
NO_MODIFICATION_ALLOWED_ERR This node is read-only and does not allow replacement, or
newChildis the child of a node that does not allow removals.-
NOT_FOUND_ERR oldChildis not a child of this node.
Description
This method replaces one node of the document tree with
another. oldChild is the node to be
replaced and must be a child of this node.
newChild is the node that takes its place
in the childNodes[] array of this
node.
If newChild is already part of the
document, it is first removed from the document before being
reinserted at its new position. If
newChild is a DocumentFragment, it is not
inserted itself; instead each of its children is inserted, in order,
at the position formerly occupied by
oldChild.
Example
The following code replaces a node n with a <b> element and then inserts the
replaced node into the <b> element, which reparents the node and makes it appear ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access