JS WEB APISChapter 7 · JS Web APIs
Finding and Changing Elements
The DOM (Document Object Model) is a web page as JavaScript sees it: a tree of elements you can read and change. document.querySelector("selector") finds the first element matching a CSS selector, and querySelectorAll finds every match. Change what an element says with .textContent, its look with .classList.add, .remove and .toggle, and its attributes with .setAttribute. The page under the editor is real: run your code and watch it change.
Worked example
How it reads
#titleselects by id and.statusby class, exactly like CSSquerySelectorAllreturns a list you can index and loop over- Changing
textContentorclassListupdates the page immediately
Common mistakes
querySelectorreturnsnullwhen nothing matches, so a typo in the selector leads to "Cannot read properties of null".

Cloud tip: Use
textContent, not innerHTML, when showing text that came from a user. It can never be run as HTML.

