← The DOM and the browser console

Click events

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.

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.

Predict

page.html
<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?

What does the heading say when the page first loads, before anyone clicks?

RunRun it and compare the result with your prediction.

Run

Press Run, then click Start and Pause. The heading changes only when you click.

page.html
<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>
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

page.html
<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.

function start() { … }
goButton.addEventListener("click", start);
"click"
message.textContent = "Go!";

Which line makes start run on every click?

This page says Go! as soon as it loads, and clicking does nothing. There is no red error. Click the line that is wrong.

ModifyChange the code on purpose and check the result.

Modify

Back to the beginning.

Need a hint?

A third button needs three things: the button in the HTML, a function, and a listener.

Another hint

Add <button id="reset">Reset</button>, a function reset that puts back "Ready?", and resetButton.addEventListener("click", reset).

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

A handler can change two things.

Need a hint?

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 result
What the finished page looks like
What 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.

      A page needs to count clicks. What does the handler need to keep between clicks?

      For teachers

      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.

      Students will be able to

      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

      1. Predict: ask what is different between writing a recipe and cooking it.
      2. Run: ask students to click each button and say which function ran.
      3. Investigate: for the bug, ask "when did the heading change, and why then?"
      4. Modify: ask where the third button is in the HTML, and where in the script.
      5. 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

      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

      Now you can
      Next lesson3. Classes and themesSwitch a whole look on and off with classList.toggle, so CSS does the styling and JavaScript only flips a class.