Add Interactivity with JavaScript
Connect a script to your page, find elements in the DOM, react to button clicks and update text, building a simple like counter step by step.
Welcome back!
Step 1 of 12Learn
Your page as a tree
HTML and CSS describe a page that sits still. JavaScript lets it react: show a message, count clicks, switch to dark mode, check a form before it's sent.
When the browser loads your HTML, it builds a live model of the page in memory called the DOM (Document Object Model). Picture a family tree: html at the top, head and body as its children, and every heading, paragraph and button as a branch further down. JavaScript can find any element in that tree and change it, and the screen updates instantly.
Add a script at the very end of the <body>, so the elements above it already exist when it runs:
<button id="like">Like</button>
<p id="count">0 likes</p>
<script src="script.js"></script>
</body>Then, in script.js, find elements with document.querySelector, which takes the same selectors as CSS:
const button = document.querySelector("#like");
const countText = document.querySelector("#count");Now button and countText are references to real elements on the page. If querySelector finds nothing, it returns null, and the next line that uses it will throw an error. A typo in the selector, like "#lke", is the number-one cause.
Step 2 of 12Practice · Diagram
A page as a DOM tree
Tap the element that document.querySelector("#score") returns.
Tap the shape that answers the question, then press Check.
- document
- html
- head
- body
- title
- h1
- p id="score"
- button id="like"
Step 3 of 12Practice · Fill in the blanks
Fill in the blanks
To find the element with id score, you write document.(""). The browser's live model of the page is called the .
Step 4 of 12Learn
Listening for events
Anything that happens on a page is an event: a click, a key press, the mouse moving, a form being submitted. You tell an element to run some code when an event happens with addEventListener:
let likes = 0;
button.addEventListener("click", function () {
likes = likes + 1;
countText.textContent = likes + " likes";
});Step by step:
let likes = 0;creates a variable that remembers a number.addEventListener("click", ...)says: whenever this button is clicked, run the function.- Inside the function, the count goes up by one and
textContentreplaces the paragraph's text.
Notice that the function is not run right away. You're handing it to the browser to keep until a click happens. That's a big shift in thinking: event-driven code waits.
Other useful events include "input" (the text in a box changed), "keydown" (a key was pressed) and "submit" (a form was sent). The pattern stays the same every time: find the element, listen for the event, change something in the DOM.
Step 5 of 12Practice · Put in order
Put the steps of a working like button in the order the code needs them.
Drag the items, or use the arrow buttons, to put them in order.
- Find the button with document.querySelector
- Add a button and a script tag at the end of the body
- Create a variable that stores the like count
- Inside the listener, add one to the count and update textContent
- Attach a click listener to the button
Step 6 of 12Diagram
What happens on a click
Tap a shape with to see more.
- User clicks
The browser creates a click event.
- Listener fires
The function you passed to addEventListener runs.
- Code updates data
likes = likes + 1
- Code updates the DOM
Change textContent of the counter.
- Page shows the change
Step 7 of 12Practice · Code challenge
The logic behind a like button can be tested without a page. Write a function likeLabel(n) that returns the text for the counter: "1 like" when n is 1, and "<n> likes" for every other number, such as "0 likes" or "5 likes".
likeLabel(0)likeLabel(1)likeLabel(5)likeLabel(21)
Step 8 of 12Note
Step 9 of 12Learn
Changing styles with classes
A favorite first project is a dark mode button. You could change colors one by one from JavaScript, but there's a cleaner way: let CSS hold the styles and let JavaScript just switch a class on and off.
In the CSS:
body.dark { background: #1e1e1e; color: #f0f0f0; }In the JavaScript:
const toggle = document.querySelector("#theme");
toggle.addEventListener("click", function () {
document.body.classList.toggle("dark");
});classList.toggle("dark") adds the class if it's missing and removes it if it's there, so each click flips the theme. The colors stay in the CSS file, where they belong, and the JavaScript stays short.
This separation of jobs is the same idea you've seen all course: HTML for content, CSS for appearance, JavaScript for behavior. When each language does its own job, every file stays easy to read and change.
One caution: JavaScript should improve a page, not hold it hostage. Your main content should still be readable if a script fails to load, so put real text in the HTML rather than inserting everything with JavaScript.
Step 10 of 12Diagram
Dark mode with one class
Tap a shape with to see more.
- Click the button
- JS toggles "dark" on body
classList.toggle adds it if missing, removes it if present.
- CSS body.dark rules apply
All dark colors live in the style sheet.
- Whole page switches
Step 11 of 12Practice · What would you do?
What would you do?
Lena's like button does nothing when clicked. The console says "Cannot read properties of null (reading 'addEventListener')". Her script tag is in the head, above all the HTML. What's the best fix?
Step 12 of 12Practice · Choose one
What does document.body.classList.toggle("dark") do when the body already has the class dark?
Lesson complete!