Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element: replacing child nodes and checking for an attribute

Use `replaceChildren()` to replace or clear an element’s child nodes, and `hasAttribute()` to check whether the element itself has an attribute, regardless of its value.

Version: Current web platform; runtime support varies

Syntax

element.replaceChildren();
element.replaceChildren(firstNodeOrString, secondNodeOrString, ...moreNodesOrStrings);
element.hasAttribute(qualifiedName);

Element.replaceChildren/hasAttribute pairs two DOM instance methods. The first changes the nodes directly under an element; the second checks an attribute on the element itself without changing the DOM. These are web platform APIs, not ECMAScript 2024 features. Check that your target runtime provides a DOM and supports both methods.

element.replaceChildren(...) replaces all direct child nodes, including text and comments. It takes any number of nodes and strings; with no arguments, it empties the element without removing it or changing its attributes. Strings become text nodes, so unlike an assignment to innerHTML, they are not parsed as HTML. The method returns undefined.

Existing nodes are moved, not cloned. A removed node is detached from its parent, but you can still use it if you kept a reference. An insertion that would create a cycle, such as making an element its own child, throws a DOMException named HierarchyRequestError.

element.hasAttribute(name) checks an attribute by its qualified name and returns true even for an empty value or the string "false". A false result means the attribute is absent, not that it has a false value. To read the value, use getAttribute(name), which returns null if the attribute is missing. To look up an attribute by namespace and local name, use hasAttributeNS(namespace, localName). For an HTML boolean attribute such as disabled, presence is what matters: setting its value to "false" does not turn it off.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Replacing child nodes and checking attribute presence</title>
</head>
<body>
<script>
(() => {
  const panel = document.createElement("div");
  const label = document.createElement("strong");
  label.textContent = "Updated";
  panel.append("Old: ", label);

  console.log(`before: ${panel.hasAttribute("data-ready")}`);
  panel.setAttribute("data-ready", "");
  panel.replaceChildren("<b>literal</b> ", label);

  console.log(`text: ${panel.textContent}`);
  console.log(`same node: ${panel.lastChild === label}`);

  panel.replaceChildren();
  console.log(`child nodes: ${panel.childNodes.length}`);
  console.log(`after: ${panel.hasAttribute("data-ready")}`);
  console.log(`value: ${JSON.stringify(panel.getAttribute("data-ready"))}`);
})();
</script>
</body>
</html>

Result

before: false
text: <b>literal</b> Updated
same node: true
child nodes: 0
after: true
value: ""

Save the example as an `.html` file, open it in a browser, and check the output in the console. The markup-like string stays literal, and `same node: true` confirms that `label` kept its identity. After the element is emptied, it still has `data-ready` with an empty value.

Related lessons

  • JavaScript · Lesson 38 · Advanced · Version: 4

Sources