Run a function when someone clicks a button: addEventListener, and the difference between handing over a function and running it.
A page that only changes when it loads is a poster. A page that changes when you click is an app.
Until now your script ran once, as the page loaded. To react to a click you give the button a function and say when to run it:
button.addEventListener("click", start);
Read it as "when this button is clicked, run start". "click" is the event. The function after it is the handler.
The hard part is the brackets. start hands the function over to be run later, on each click. start() runs it right now, once, and hands over whatever it returned, so the click does nothing.
Make your prediction first. Help opens once you've locked it in.
<h1 id="message">Ready?</h1>
<button id="go">Start</button>
<script>
const message = document.querySelector("#message");
const goButton = document.querySelector("#go");
function start() {
message.textContent = "Go!";
}
goButton.addEventListener("click", start);
</script>
Which line decides when start runs?
Press Run, then click Start and Pause. The heading changes only when you click.
<h1 id="message">Ready?</h1>
<button id="go">Start</button>
<button id="pause">Pause</button>
<script>
const message = document.querySelector("#message");
const goButton = document.querySelector("#go");
const pauseButton = document.querySelector("#pause");
function start() {
message.textContent = "Go!";
}
function pause() {
message.textContent = "Paused";
}
goButton.addEventListener("click", start);
pauseButton.addEventListener("click", pause);
</script>
<h1 id="message">Ready?</h1>
<button id="go">Start</button>
<button id="pause">Pause</button>
<script>
const message = document.querySelector("#message");
const goButton = document.querySelector("#go");
const pauseButton = document.querySelector("#pause");
function start() {
message.textContent = "Go!";
}
function pause() {
message.textContent = "Paused";
}
goButton.addEventListener("click", start);
pauseButton.addEventListener("click", pause);
</script>
Match code with meaning.
One line decides.
No error, and nothing works.
Match each piece with what it does.
This page says Go! as soon as it loads, and clicking does nothing. There is no red error. Click the line that is wrong.
Back to the beginning.
A third button needs three things: the button in the HTML, a function, and a listener.
Add <button id="reset">Reset</button>, a function reset that puts back "Ready?", and resetButton.addEventListener("click", reset).

A handler can change two things.
A handler can change more than one thing. What else about the heading can you change?
Add message.style.color = "green"; inside start, and another colour inside pause.

Add a Reset button that puts the heading back to Ready?
This ticks when your page has all of the above.
Make Start turn the heading green and Pause make it orange.
This ticks when your page has all of the above.
Start from the two buttons and make a page where each button changes something different: size, colour, words, or a picture. The page on this lesson makes a heading bigger, smaller or purple.
You can now run code on a click. Next: change how the whole page looks with classes.
Students run a function when a button is clicked with addEventListener, tell handing over a function from running it, fix a listener written with brackets, add a Reset button, and make buttons that change different things. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. This is the Python events lesson (a handler runs when something happens) in JavaScript, and the same lesson as p5's mousePressed, but the page picks the element and the event. addEventListener("click", start) hands the browser a function; start() calls it now. The bug has no red error, so students must look at the page: it says Go! straight away. Functions were taught in the p5.js course; if students skipped it, spend a minute on "a function is code with a name" first. IDs named like variables (message) are fine here because the script uses const.
Brackets: start hands the function over, start() runs it now. The page, not an error, shows the slip.
Stretch: Make one button toggle between two states using a variable, or make a button remove itself with button.remove().
Support: Give the whole Reset function and listener, and ask students to change what it says.
Next lesson: classes, to change how the whole page looks with one line.