From Zero to Deploy · Module 2 · Lesson 2.5
2.5 Events and the page, making clicks do things
How JavaScript finds an element on the page, changes its text, and reacts to clicks and to what the person types.
The page is made of elements
So far the code has only talked to the console. In a real app, it changes the page: it swaps a piece of text, shows a number, paints the background. The page is made of elements written in HTML, like <h1> (a heading), <p> (a paragraph), and <button> (a button).
For JavaScript to change an element, it first has to find it. The most common way is to give the element an id (a unique name) in the HTML and look it up with document.getElementById:
This example has two parts: the HTML (what shows on the page) and the JavaScript (what changes). textContent is the text inside the element. Putting a value into it changes what shows on screen.
Challenge: change the paragraph’s text too. Its id is info.
addEventListener: “when this happens, do that”
A page that changes by itself isn’t much use. What matters is reacting to what the person does: a click, a key, some typed text. Each of these is an event. And addEventListener says: when this event happens on this element, run this function.
Click the button a few times. Notice that the function doesn’t run when the page opens: it waits, and runs once per click. It’s an arrow function, just like the ones in the functions lesson.
Challenges:
- Add a reset button. In the HTML:
<button id="reset">Reset</button>. In the JavaScript, a secondaddEventListeneron that button that setsvisits = 0and updates thecounter. - When visits reach 5, show “You get a free coffee!” in the console. You need an
ifinside the click function.
Now it’s your turn. Write the code in the editor and click Run tests.
“Hello, world!” again
Remember the button that changes the background color, from the first module? Now you can read every line of it:
const colors = [...]is a list of colors.let index = 0is a variable that changes: it keeps the position of the current color.addEventListener('click', () => { ... })is an event that runs a function on every click.index = (index + 1) % colors.lengthadds 1, and%(the remainder of a division) brings it back to 0 after the last color.document.body.style.background = colors[index]takes the color at that position and paints the background.
Challenges:
- Add a new color to the list, for example
'#c7d2fe'. Nothing else needs to change, because the code usescolors.length. - Make the heading change too. In the HTML, give the heading an id:
<h1 id="title">. In the JavaScript, inside the click, change itstextContentto`Color ${index + 1} of ${colors.length}`.
Reacting to what the person types
Clicks aren’t the only event. The input event happens on every letter typed into a field, and .value is the text in the field at that moment:
Type your name, then delete it all. The if from the conditions lesson decides which greeting to show.
Challenge: if the name has more than 10 letters, show “What a long name!” at the end of the greeting. A piece of text also has .length, just like a list.
The button that does nothing
This is the bug you’ll run into most at the start: you click, and nothing happens.
Where you’ll see this in the course
- In the 3D game, the
keydownandkeyupevents say when a key was pressed and released. The game keeps track of it in an object,keys[e.code] = true, and on every frame checks which arrows are held down. On a phone, the on-screen buttons use thepointerdownevent to do the same. - In the map app, the check-in buttons live inside the map’s popups. Instead of putting an event on each button, the app listens to clicks on the whole page with
document.addEventListener('click', ...)and checks which button was clicked. - In the AI app, the choose-photo field uses the
changeevent (written asonchange), which happens when the person picks a file.
End of the module
You’ve learned the five ideas that show up in every app: values and variables, functions, conditions and loops, lists and objects, and events. You don’t need to write a whole app by yourself, but now you can read the code the AI writes, understand what each part does, and notice when something is wrong.
In the next module, Spec first, you learn the method that makes the AI build exactly what you want.
Mark it complete to track your progress.