Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element.replaceChildren() and Element.className

Replace or clear all of an element’s child nodes, and read or set its entire `class` attribute without changing its children.

Version: Current web platform; runtime support varies

Syntax

element.replaceChildren();
element.replaceChildren(node, "text");
element.replaceChildren(...items); // Spread an iterable of nodes and/or strings.
const classes = element.className; // Read the class attribute of an HTML element.
element.className = "panel ready"; // Replace the entire class attribute value.

Element.replaceChildren/className pairs two independent DOM operations. The method element.replaceChildren(...items) replaces all direct child nodes, including text nodes, and returns undefined. It accepts any number of Node objects and strings in the same call. Strings become text nodes; they are not parsed as HTML. Calling it with no arguments empties the element without removing the element itself or changing its attributes.

Existing nodes are moved, not cloned. A detached node can be reused if you keep a reference to it. Breaking a DOM tree rule, for example by trying to insert an ancestor as the element’s child, throws a DOMException named HierarchyRequestError.

On an HTML element, the element.className property reflects the class attribute as a string of whitespace-separated class names. Assigning to it replaces the entire value without changing any child nodes. To change individual classes without replacing the rest, use classList.add(), classList.remove(), or classList.toggle(). On SVG elements, className can be an SVGAnimatedString object; use getAttribute('class') or setAttribute('class', value) to read or write the attribute itself.

In the version label, ECMAScript 2024 is only the JavaScript language baseline. These are web-platform DOM APIs, not ECMAScript 2024 features. Availability depends on the runtime; an environment without a DOM does not provide them on its own.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Element.replaceChildren() and Element.className</title>
</head>
<body>
<script>
{
  const panel = document.createElement("div");
  panel.className = "panel loading";
  panel.textContent = "Loading…";

  const label = document.createElement("span");
  label.textContent = "Ready";
  panel.replaceChildren(label, " <pending>");
  panel.className = "panel ready";
  console.log(`${panel.childNodes.length} | ${panel.textContent} | ${panel.className}`);

  panel.replaceChildren();
  console.log(`${panel.childNodes.length} | ${panel.className}`);
}
</script>
</body>
</html>

Result

2 | Ready <pending> | panel ready
0 | panel ready

Save the example as an HTML file, open it in a browser, and check the browser console. The first line shows two child nodes: the `span` element and the literal text ` <pending>`. The second shows that clearing the children leaves the `class` attribute set to `panel ready`.

Related lessons

  • JavaScript · Lesson 43 · Intermediate · Version: 3

Sources