LOGBOOK

HELP

Quiz Entry - updated: 2026.06.20

How do you select elements by class name in JavaScript?

Use getElementsByClassName("name") (no dot, live HTMLCollection) or querySelectorAll(".name") (with dot, static NodeList).

// getElementsByClassName - returns live HTMLCollection
const items = document.getElementsByClassName("item");

// querySelectorAll - returns static NodeList
const cards = document.querySelectorAll(".card");

Iterating through results:

// HTMLCollection - use for...of or convert to array
for (const item of items) {
    console.log(item);
}

// NodeList - has forEach
cards.forEach(card => console.log(card));

Note: getElementsByClassName does NOT use a dot prefix, while querySelectorAll uses CSS selector syntax (with dot).

// No dot
getElementsByClassName("myClass");
// With dot
querySelectorAll(".myClass");

From Quiz: WEBT / Frontend | Updated: Jun 20, 2026