JavaScript: querySelectorAll

querySelectorAll() returns a collection of every element that matches a CSS selector.

What you will learn

Minimal example

const cards = document.querySelectorAll('.card');
cards.forEach(card => {
  card.classList.add('visible');
});

If there are no matches, the result is an empty NodeList, so a loop simply performs no work. The collection does not automatically include elements added later.

Common mistakes