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.
What you should already know:
Find and change· Find an element on a page with JavaScript, change its text and colour, and read what the console says.
Forms· Build a form from labels, boxes and a button, and find out it collects answers but does nothing alone.
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.
How this lesson works: Predict, Run, Investigate, Modify, Make
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
A handler can change more than one thing. What else about the heading can you change?
Another hint
Add message.style.color = "green"; inside start, and another colour inside pause.
Show the expected resultWhat it should look like. Your words can be different.
Try it yourselfYour edits are saved in this browser.
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.
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Buttons of your own
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.
Try it yourselfYour edits are saved in this browser.
Info
Where this leads
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.
I can run a function when someone clicks a button.
I can say what the event name and the handler are.
I can give a handler without running it straight away.
Before class
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.
Running the lesson
Predict: ask what is different between writing a recipe and cooking it.
Run: ask students to click each button and say which function ran.
Investigate: for the bug, ask "when did the heading change, and why then?"
Modify: ask where the third button is in the HTML, and where in the script.
Make: ask each pair which button does what, before they test.
The hard part (on purpose)
Brackets: start hands the function over, start() runs it now. The page, not an error, shows the slip.
When students get stuck
The heading says Go! at once and the button does nothing. Ask: Are there brackets after start in the listener? Fix: Remove them: addEventListener("click", start).
Cannot read properties of null (reading addEventListener). Ask: Does the button id in the HTML match the selector? Fix: Match the id exactly, with # in the selector.
Nothing happens when I click. Ask: Did you press Run, and is the button inside the page? Fix: Press Run again; check the id.
The new button does nothing. Ask: Did you add a listener for it? Fix: Add its listener under the others.
Stretch and support
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.
Where it leads
Next lesson: classes, to change how the whole page looks with one line.
What each step is for
Investigate: addEventListener hands a function to the browser to run later, on each event; writing the function name with brackets runs it now instead.
Modify: Each button needs its own function and its own listener; a handler can change more than one thing.
buttons of your own: Goals only: at least two buttons that change different things, each with its own function.