Document: replaceChildren() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2020.

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

Syntax

js
replaceChildren(param1)
replaceChildren(param1, param2)
replaceChildren(param1, param2, /* …, */ paramN)

Parameters

param1, …, paramN

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

Return value

None (undefined).

Exceptions

HierarchyRequestError DOMException

Thrown if the constraints of the node tree are violated.

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:

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

Specifications

Specification
DOM
# ref-for-dom-parentnode-replacechildren①

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
replaceChildren
Chrome – Full support
Chrome 86 (Release date: 2020-10-20)
footnote Full support
Edge – Full support
Edge 86 (Release date: 2020-10-09)
footnote Full support
Firefox – Full support
Firefox 78 (Release date: 2020-06-30)
footnote Full support
Opera – Full support
Opera 72 (Release date: 2020-10-21)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Chrome Android – Full support
Chrome Android 86 (Release date: 2020-10-20)
footnote Full support
Firefox for Android – Full support
Firefox for Android 79 (Release date: 2020-07-28)
footnote Full support
Opera Android – Full support
Opera Android 61 (Release date: 2020-12-07)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14 (Release date: 2020-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 14 (Release date: 2021-04-17)
footnote Full support
WebView Android – Full support
WebView Android 86 (Release date: 2020-10-20)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14 (Release date: 2020-09-16)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also