Document.replaceChildren()

The Document.replaceChildren() method replaces the existing children of a Document with a specified new set of children.

Syntax

replaceChildren(...nodesOrDOMStrings)

Parameters

nodesOrDOMStrings
A set of Node or DOMString objects to replace the Document's existing children with. If no replacement objects are specified, then the Document is emptied of all child nodes.

Exceptions

Examples

Emptying a document

replaceChildren() provides a very convenient mechanism for emptying a document of all its children. You call it on the document without any argument specified:

document.replaceChildren();
document.children; // HTMLCollection []

Specifications

Specification
DOM
The definition of 'ParentNode.replaceChildren()' in that specification.

Browser compatibility

BCD tables only load in the browser

See also