files/en-us/web/api/documentfragment/replacechildren/index.md
{{APIRef("DOM")}}
The DocumentFragment.replaceChildren() method replaces the
existing children of a DocumentFragment with a specified new set of children. These
can be string or {{domxref("Node")}} objects.
replaceChildren(param1)
replaceChildren(param1, param2)
replaceChildren(param1, param2, /* …, */ paramN)
param1, …, paramN
DocumentFragment's existing children with. If no replacement objects are
specified, then the DocumentFragment is emptied of all child nodes.None ({{jsxref("undefined")}}).
HierarchyRequestError {{DOMxRef("DOMException")}}
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:
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 []
{{Specifications}}
{{Compat}}