Web APIs

Element.replaceChildren()

Try the free JavaScript compiler

Replace all children with the supplied nodes or text. With no arguments, empty the element. Example console output: new 0.

Formal Syntax & Structure

javascript syntaxjavascript
element.replaceChildren(...nodes)

Parameters & Attributes

NameTypeDescription
nodes(Node or string)[]Variadic replacement nodes or text.

Return Value

undefined

No result value.

Working Example

Example Snippetjavascript
const p=document.createElement("p"); p.append(document.createElement("b")); p.replaceChildren("new"); console.log(p.textContent,p.children.length);
JavaScript Reference