JavaScript AcademyProfessional Engineering

Learning object 37 of 100

CSS selector explorer

Type a CSS selector and see exactly which elements querySelectorAll finds, in document order, and what querySelector would return.

Loading…

What it shows

querySelector and querySelectorAll take the same selectors as CSS: tag names, .classes, #ids, [attributes], combinators such as a space (descendant) and > (child), and pseudo-classes such as :first-child, :not() and :disabled. querySelector returns the first match or null; querySelectorAll returns a static NodeList of every match. An invalid selector throws a SyntaxError.

Guided experiment

  1. Try .item and then .item.sale. Why does adding a class make the match narrower?
  2. Compare #shop .price with ul > li. What does each combinator mean?
  3. Pick li[. What happens with an invalid selector?

Predict

What does document.querySelector(".missing") return when nothing matches?

Related

Lessons: Lesson 52: Selecting elements

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-037.js in the companion repository.