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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min
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.
| What you will see | A question to ask | The fix |
|---|---|---|
| The heading says Go! at once and the button does nothing. | Are there brackets after start in the listener? | Remove them: addEventListener("click", start). |
| Cannot read properties of null (reading addEventListener). | Does the button id in the HTML match the selector? | Match the id exactly, with # in the selector. |
| Nothing happens when I click. | Did you press Run, and is the button inside the page? | Press Run again; check the id. |
| The new button does nothing. | Did you add a listener for it? | Add its listener under the others. |
One possible solution for each task. Other correct programs are fine.
<h1 id="message">Ready?</h1>
<button id="go">Start</button>
<button id="pause">Pause</button>
<button id="reset">Reset</button>
<script>
const message = document.querySelector("#message");
const goButton = document.querySelector("#go");
const pauseButton = document.querySelector("#pause");
const resetButton = document.querySelector("#reset");
function start() {
message.textContent = "Go!";
}
function pause() {
message.textContent = "Paused";
}
function reset() {
message.textContent = "Ready?";
}
goButton.addEventListener("click", start);
pauseButton.addEventListener("click", pause);
resetButton.addEventListener("click", reset);
</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!";
message.style.color = "green";
}
function pause() {
message.textContent = "Paused";
message.style.color = "orange";
}
goButton.addEventListener("click", start);
pauseButton.addEventListener("click", pause);
</script>
<h1 id="message">My buttons</h1>
<button id="one">Bigger</button>
<button id="two">Smaller</button>
<button id="three">Colour</button>
<script>
const message = document.querySelector("#message");
let size = 32;
function bigger() {
size = size + 8;
message.style.fontSize = size + "px";
}
function smaller() {
size = size - 8;
message.style.fontSize = size + "px";
}
function colour() {
message.style.color = "rebeccapurple";
}
document.querySelector("#one").addEventListener("click", bigger);
document.querySelector("#two").addEventListener("click", smaller);
document.querySelector("#three").addEventListener("click", colour);
</script>
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.
What is the difference between start and start() in a listener?
start hands the function over to run on each click; start() runs it once, right now.
Next lesson: classes, to change how the whole page looks with one line.