Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element: select descendants, check attributes, and measure rectangles

Use `querySelectorAll()` to find matching descendants, `hasAttribute()` to check whether an attribute is present, and `getClientRects()` to read layout geometry. None of these results alone tells you whether the user can see an element.

Version: Current web platform; runtime support varies

Syntax

element.querySelectorAll(selectors) → NodeList
element.hasAttribute(name) → boolean
element.getClientRects() → DOMRectList

Element.querySelectorAll/hasAttribute/getClientRects groups three methods that answer different questions: what matches in the DOM, what attribute is present, and what rectangles the layout produces. These are web-platform APIs, not features of ECMAScript 2024. The example needs a browser with a DOM and a layout engine.

element.querySelectorAll(selectors) takes a string containing a CSS selector or selector list. It returns a static NodeList of matching descendants in document order, excluding the element you call it on. No matches gives an empty list; invalid selector syntax throws a DOMException named SyntaxError. DOM changes do not update the saved list's membership, but its entries still refer to the original nodes. Ancestors outside the element can participate in selector matching; :scope > .card selects only its direct children with the .card class. Use CSS.escape() on a dynamic identifier, not on the whole selector.

element.hasAttribute(name) checks for a named attribute on that element: it returns true even if the value is empty, and false if the attribute is absent. It does not read the value or check descendants. Use getAttribute(name) to read the value. To match by namespace and unprefixed local name, use hasAttributeNS(namespace, localName).

element.getClientRects() takes no arguments. For an HTML element it generally returns a collection of DOMRect objects, one per CSS border-box fragment. An inline element split across lines can have several rectangles. Coordinates are in CSS pixels relative to the viewport and account for scrolling. Measure again after layout changes. If you want one enclosing rectangle instead, use getBoundingClientRect().

display: none gives an empty rectangle collection but does not remove the element from the DOM or prevent a selector match. A nonempty collection does not prove visibility: the element might be offscreen or have visibility: hidden. hasAttribute("hidden") checks only for the HTML attribute, not the rendering state.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Element layout example</title>
</head>
<body>
<script>
(() => {
  const root = document.createElement("section");
  const shown = document.createElement("div");
  const hidden = document.createElement("div");
  shown.className = hidden.className = "card";
  shown.setAttribute("data-flag", "");
  shown.style.cssText = "display: block; width: 10px; height: 10px";
  hidden.style.display = "none";
  root.append(shown, hidden);
  document.body.append(root);

  try {
    const selector = ":scope > .card";
    const cards = root.querySelectorAll(selector);
    for (const card of cards) {
      console.log(`flag=${card.hasAttribute("data-flag")} boxes=${card.getClientRects().length}`);
    }
    hidden.remove();
    console.log(`saved=${cards.length} fresh=${root.querySelectorAll(selector).length}`);
  } finally {
    root.remove();
  }
})();
</script>
</body>
</html>

Result

flag=true boxes=1
flag=false boxes=0
saved=2 fresh=1

Save the example as an HTML file, open it in a browser, and check the developer console. Both children match: the first has an empty `data-flag` attribute and one rectangle; the second lacks the attribute and has zero rectangles because of `display: none`. After removal, the saved list still has two entries, but a fresh query returns one.

Related lessons

  • JavaScript · Lesson 40 · Intermediate · Version: 3

Sources