JavaScript Element replaceChildren() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.replaceChildren() is an instance method from the ParentNode API. It replaces all existing children with a new set of nodes or strings. Learn how to empty a container, transfer nodes between lists (MDN party food demo), how it compares to append() and innerHTML, and five try-it labs.

01

Kind

Instance method

02

Action

Replace all kids

03

Args

Nodes or strings

04

Empty

No arguments

05

vs append

Adds only

06

Status

Baseline widely

Introduction

Old patterns clear children with loops or innerHTML = "". replaceChildren() is the modern ParentNode helper: swap the entire child list in one call—or empty the element by calling it with no arguments (MDN).

Like append() and prepend(), strings become Text nodes automatically.

💡
Beginner tip

MDN: myNode.replaceChildren() with no args is a convenient way to empty a node of all its children—cleaner than manual loops.

This page is part of JavaScript Element. Related: append(), prepend(), remove().

Understanding the replaceChildren() Method

Calling el.replaceChildren(...nodes) removes every current child of el, then inserts the arguments as the new child list in order.

  • It is an instance method on Element (ParentNode mixin).
  • Arguments can be Node objects or strings (as Text).
  • No arguments → element is emptied (MDN).
  • Existing nodes in the document are moved, not cloned.
  • Return value is undefined (MDN).
  • Can throw when node tree constraints are violated (MDN).

📝 Syntax

General forms of Element.replaceChildren (MDN):

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

Parameters

  • param1, …, paramN (optional) — a set of Node objects or strings to replace the element’s existing children with (MDN). If omitted, all children are removed.

Return value

None (undefined) (MDN).

Exceptions

  • Thrown when the constraints of the node tree are violated (MDN).

Common patterns

JavaScript
// Empty all children (MDN)
myNode.replaceChildren();

// Replace with new content
container.replaceChildren(heading, paragraph, " ");

// Transfer nodes between parents (MDN party food pattern)
yesSelect.replaceChildren(...selected, ...existing);

⚡ Quick Reference

GoalCode
Empty all childrenel.replaceChildren()
Replace with nodesel.replaceChildren(a, b)
Replace with textel.replaceChildren("Hello")
Transfer nodesel.replaceChildren(...nodes)
Return valueundefined
MDN statusBaseline Widely available (since October 2020)

🔍 At a Glance

Four facts to remember about Element.replaceChildren().

Returns
undefined

Updates the DOM in place

Baseline
widely

Since Oct 2020

Empty
()

No arguments

Strings
→ Text

Auto Text nodes

📋 replaceChildren() vs append() vs innerHTML

replaceChildren()append()innerHTML = ""
Existing childrenRemoved firstKeptReplaced (parsed HTML)
Accepts strings?Yes (as Text)Yes (as Text)HTML string only
Empty containerreplaceChildren()No effect aloneinnerHTML = ""
Return valueundefinedundefinedString assigned
Best forSwap all childrenAdd at endHTML string templates

MDN highlights replaceChildren() for emptying nodes and transferring children between parents without verbose loop code.

Examples Gallery

Examples follow MDN Element.replaceChildren() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Empty a container and replace all children—MDN’s core patterns.

Example 1 — Empty a Node (MDN)

Call replaceChildren() with no arguments to remove all children.

JavaScript
const myNode = document.getElementById("box");
myNode.replaceChildren();

console.log(myNode.childNodes.length); // 0
Try It Yourself

How It Works

MDN: this is a convenient mechanism for emptying a node without loops or innerHTML = "".

Example 2 — Replace With New Children

Swap the entire child list for new nodes and text.

JavaScript
const container = document.getElementById("box");
const heading = document.createElement("h2");
heading.textContent = "Updated";
const note = document.createElement("p");
note.textContent = "Fresh content";

container.replaceChildren(heading, note);

console.log(container.children.length); // 2
Try It Yourself

How It Works

Old children are removed first. The heading and paragraph become the only children, in argument order.

📈 Practical Patterns

MDN transfer demo, text nodes, and comparison with append.

Example 3 — Transfer Nodes Between Lists (MDN)

Move selected options into another select with the spread operator.

JavaScript
const noSelect = document.getElementById("no");
const yesSelect = document.getElementById("yes");

const selected = document.querySelectorAll("#no option:checked");
const existing = document.querySelectorAll("#yes option");

yesSelect.replaceChildren(...selected, ...existing);
Try It Yourself

How It Works

MDN: replaceChildren() lets you transfer nodes between elements without verbose looping. Selected options move to the “yes” list.

Example 4 — Replace With a String

Strings become Text nodes, like append().

JavaScript
const div = document.createElement("div");
div.append(document.createElement("p")); // old child

div.replaceChildren("Hello, world!");

console.log(div.textContent); // "Hello, world!"
Try It Yourself

How It Works

The previous <p> is removed. The string becomes the sole Text child.

Example 5 — vs append()

append() keeps existing children; replaceChildren() clears first.

JavaScript
const box = document.getElementById("box");
const extra = document.createElement("span");
extra.textContent = "new";

box.append(extra);
console.log(box.childNodes.length); // 3 (kept old + added)

box.replaceChildren(extra);
console.log(box.childNodes.length); // 1 (only extra)
Try It Yourself

How It Works

Use append() to add at the end. Use replaceChildren() when you want a completely new child list.

🚀 Common Use Cases

  • Emptying modal bodies, lists, or panels before re-rendering content.
  • Transferring selected options between dual-list UIs (MDN party food demo).
  • Replacing a container’s children with freshly built nodes in one call.
  • Avoiding while (el.firstChild) el.removeChild(...) loops.
  • Teaching ParentNode helpers alongside append() and prepend().
  • Pairing with remove() when you need to delete the element itself.

🧠 How replaceChildren() Swaps Child Lists

1

Call on the parent element

el.replaceChildren(...) on the container whose children you want to replace.

Parent
2

Remove existing children

All current child nodes are detached from the element (MDN).

Clear
3

Insert new children

Arguments become the new child list—strings as Text nodes, in order.

Insert
4

DOM updated; return undefined

Check childNodes.length or children—no return payload.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available since October 2020).
  • replaceChildren() replaces all children—not siblings.
  • Return value is always undefined (MDN).
  • No arguments empties the element (MDN).
  • Invalid hierarchy can throw per MDN node tree constraints.
  • Related: append(), prepend(), children, JavaScript hub.

Browser Support

Element.replaceChildren() is Baseline Widely available (MDN: across browsers since October 2020). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.replaceChildren()

Replace all child nodes at once — empty with no args, or pass nodes and strings.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Not supported
No
replaceChildren() Excellent

Bottom line: Use replaceChildren() to empty containers or swap entire child lists. Use append() when you only need to add at the end.

Conclusion

Element.replaceChildren() is the modern way to swap an element’s entire child list—or empty it with no arguments. It returns undefined and updates the DOM in place.

Continue with replaceWith(), append(), prepend(), children, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use replaceChildren() to empty a container (MDN)
  • Pass nodes and strings like append()
  • Use spread (...) to transfer NodeLists
  • Prefer it over manual removeChild loops
  • Verify with childNodes.length

❌ Don’t

  • Use when you only need to add at the end (append)
  • Expect a returned node (it is undefined)
  • Confuse with replaceWith() (replaces the element itself)
  • Forget removed children are detached from the parent
  • Assume IE supports replaceChildren()

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.replaceChildren()

Replace all children in one call.

5
Core concepts
📄 02

Args

nodes + text

Flexible
✍️ 03

Transfer

spread ...

MDN
04

vs append

clears first

Compare
05

Baseline

since 2020

Status

❓ Frequently Asked Questions

It replaces the existing children of an element with a new set of child nodes or strings (MDN). If no arguments are passed, the element is emptied of all child nodes.
No. MDN marks Element.replaceChildren() as Baseline Widely available (across browsers since October 2020). It is not Deprecated, Experimental, or Non-standard.
Call replaceChildren() with no arguments: el.replaceChildren(). MDN describes this as a convenient way to remove all children.
Zero or more Node objects or strings. Strings are inserted as equivalent Text nodes, like append() and prepend() (MDN).
Nothing useful — the return value is undefined (MDN).
append() adds after the last child. replaceChildren() removes all existing children first, then inserts the new set.
Did you know?

MDN’s party food demo shows how replaceChildren(...selected, ...existing) transfers <option> nodes between two <select> lists in one call—no manual loop required.

More Element Topics

Browse Element methods and properties to keep building DOM skills.

replaceWith() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful