The DocumentFragment.replaceChildren()
method replaces the existing children of a DocumentFragment
with a specified new set of children. These can be string or Node
objects.
The DocumentFragment.replaceChildren()
method replaces the existing children of a DocumentFragment
with a specified new set of children. These can be string or Node
objects.
js
replaceChildren(param1) replaceChildren(param1, param2) replaceChildren(param1, param2, /* …, */ paramN)
None (undefined
).
HierarchyRequestError
DOMException
Thrown when the constraints of the node tree are violated.
replaceChildren()
provides a very convenient mechanism for emptying a document fragment of all its children. You call it on the document fragment without any argument specified:
js
let fragment = new DocumentFragment(); let div = document.createElement("div"); let p = document.createElement("p"); fragment.append(p); fragment.prepend(div); fragment.children; // HTMLCollection [<div>, <p>] fragment.replaceChildren(); fragment.children; // HTMLCollection []
Specification |
---|
DOM Standard # ref-for-dom-parentnode-replacechildren① |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
replaceChildren |
86 | 86 | 78 | No | 72 | 14 | 86 | 86 | 79 | 61 | 14 | 14.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment/replaceChildren