From 4edec56462231265fea51f377bb798a3f242c92a Mon Sep 17 00:00:00 2001 From: Jamie York Date: Thu, 6 Jun 2024 14:28:46 +0100 Subject: [PATCH] Use replaceChildren() to clear child notes The `replaceChildren()` node with no parameters can be used to quickly clear a node's children: https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren#emptying_a_node --- contents/remove-all-children-of-a-node.mdx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/contents/remove-all-children-of-a-node.mdx b/contents/remove-all-children-of-a-node.mdx index 8dc3485..130e5a8 100644 --- a/contents/remove-all-children-of-a-node.mdx +++ b/contents/remove-all-children-of-a-node.mdx @@ -23,6 +23,14 @@ while (node.firstChild) { } ``` +## 3. Replace the child nodes + +Replace its child nodes with... nothing: + +```js +node.replaceChildren(); +``` + ## See also - [Remove an element](https://phuoc.ng/collection/html-dom/remove-an-element/)