Example 1 — Collect Matches (MDN)
Get every .item under a list and report the count.
const list = document.getElementById("list");
const out = document.getElementById("out");
const matches = list.querySelectorAll(".item");
out.textContent = "count: " + matches.length; How It Works
- Scope to a parent. Search only under
#list. - Pass a CSS selector.
".item"is valid CSS. - Get a NodeList. Every match is included.
- Read
length—0means no matches.

