Web DesignMaking it moveLesson 9 / 69

Events & a click counter

Code Lab40 min

Events & a click counter

Events are how the page hears the user. You register a listener and give it a function to run when the event fires:

let count = 0;
const btn = document.querySelector("#clicker");
const out = document.querySelector("#count");

btn.addEventListener("click", () => {
  count = count + 1;
  out.textContent = count;
});

Read it slowly: an event fires → your function runs → the function changes the DOM → the user sees it. Every interaction you have ever used on the web is this loop, at speed.

Exercise

Build a click counter with a reset button. Then make the number bold and larger every time it passes 10.

Practice this lesson in the IDE

Open the Web Design practice workspace, edit the starter code, and run your work in the browser.

Open IDE and practice