Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1022 Bytes

README.md

File metadata and controls

37 lines (24 loc) · 1022 Bytes

25. Selecting DOM elements

Find and inspect live page elements.

Learn

  • querySelector
  • querySelectorAll
  • Element
  • NodeList

How it works

The DOM represents the parsed document as objects. querySelector returns the first match or null; querySelectorAll returns a static collection of matches. A selector is a CSS selector string, so choose stable classes or data attributes rather than fragile ancestry.

Common mistake

Calling a method on null throws when the selector matches nothing.

Guided challenge

Select a heading, button, and list; print their text and count, then deliberately test a missing selector.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.