What the DOM actually is
DOM stands for Document Object Model โ it's the browser's live, in-memory representation of your HTML page. When JavaScript "selects an element," it's grabbing a piece of this model, and any change you make updates what the user sees instantly.
Selecting elements
querySelector grabs the first matching element. querySelectorAll grabs all of them, as a list you can loop through with .forEach() โ exactly like the arrays from Lesson 3.
Changing text content
textContent to change plain text. Avoid innerHTML unless you specifically need to insert HTML tags โ it can accidentally run malicious code if the text ever comes from user input, a security issue called XSS.
Changing styles from JavaScript
You can set any CSS property this way โ note that hyphenated CSS properties become camelCase in JavaScript: background-color becomes backgroundColor.
Adding and removing classes (the preferred approach)
For most real projects, this is better than setting .style directly: define the look in your CSS file with a class, then just toggle that class on and off from JavaScript. It keeps your styling in one place instead of scattered across your script.
See it live
This button is running real JavaScript right now, on this page:
Try it yourself
document.querySelector to grab both, and write a function that changes the paragraph's text and color when the button is clicked (you'll wire up the actual click in Lesson 5 โ for now, just call the function directly in the console to test it).
Level up: toggle a class
.hidden { display: none; } class in your CSS. Select an element and use classList.toggle("hidden") in the console to make it appear and disappear. This exact pattern is behind almost every "show/hide" UI element you've ever used.
1. What does DOM stand for?
2. Which is safer for changing plain text on a page?
3. What does classList.toggle("active") do?
querySelector grabs elements, textContent changes their text, and classList is the preferred way to control their styling from your script.