๐Ÿ”’ Members-only lesson โ€” thanks for supporting devnotes
← back to course
progress is saved in this browser โ€” no account needed
devnotes / JavaScript Basics / Lesson 4
lesson 4 of 6 ยท premium

The DOM: Making Pages Interactive

20 min ยท JavaScript Basics

Everything so far has run only in the console. This lesson is where JavaScript starts touching the actual page โ€” selecting elements, changing their text, and updating styles live, without reloading anything.

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

// the modern, most flexible way โ€” works with any CSS selector const title = document.querySelector("h1"); const allCards = document.querySelectorAll(".card"); // older, still common for a single element by ID const header = document.getElementById("header");

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

const title = document.querySelector("h1"); title.textContent = "New heading text!";
quick tip
Use 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.
AD SLOT โ€” 728ร—90 or responsive (mid-lesson)

Changing styles from JavaScript

const box = document.querySelector(".box"); box.style.backgroundColor = "orange"; box.style.padding = "20px";

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)

box.classList.add("active"); /* add a class */ box.classList.remove("active"); /* remove a class */ box.classList.toggle("active"); /* on if off, off if on */

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:

Nothing has changed yet.

Try it yourself

exercise
In your HTML page, add a button and a paragraph. Use 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

exercise 2
Create a .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.
โœ“ quick check โ€” 3 questions

1. What does DOM stand for?

2. Which is safer for changing plain text on a page?

3. What does classList.toggle("active") do?

what's next
Lesson 5 covers events and forms โ€” how to actually respond to clicks, typing, and form submissions, instead of testing everything manually in the console.
bottom line
The DOM is the live version of your page that JavaScript can read and change. querySelector grabs elements, textContent changes their text, and classList is the preferred way to control their styling from your script.
AD SLOT โ€” 300ร—250 or responsive (in-lesson)
next upLesson 5: Events and Forms
Continue โ†’