Skip to content

Events & State

The Course
•
14 min study

A program that runs once can produce a report. An interactive page also responds later, when a person clicks, types, or submits a form. Those actions create events. addEventListener("click", callback) registers a function to run for each click. Pass the function itself; writing callback() would call it immediately instead.

Keep state, the application's current data, separate from rendering, the code that displays it. For a counter: state is a number; an event changes the number; render writes the number to textContent. Do not use the visible label as the only storage for the count.

Use an actual button, not a clickable paragraph. Buttons support keyboard activation automatically. Give the output an aria-live attribute so assistive technology can announce updates. These small choices make the same interaction available to more people.

Read the button and listener together

In the lab, <button id="add" type="button">Add 5 minutes</button> creates a button. Its id lets the script find it; type=button gives it a plain action rather than form submission.

The script defines addFive, then registers it with document.querySelector("#add").addEventListener("click", addFive). The browser keeps that function and calls it later for each click. The final render() at the bottom runs immediately to show the initial zero. That gives three separate jobs: find the button, register the future action, and display the starting state.

aria-live="polite" is an HTML attribute on the output paragraph. It asks screen readers to announce changed text when they can do so without interrupting the user.

Predict before running

The console example models two clicks by calling a function twice. In the webpage lab below, the browser calls the handler in response to the real button. What is the final value?

Predict, then run JavaScript
let minutes = 0;

function addFive() {
  minutes = minutes + 5;
}

addFive();
addFive();
console.log(minutes);
Check your prediction
Text
10

Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.

State changes first, display follows

A click is an event: the browser calls your listener when it happens. Pass a function to addEventListener; do not call it while registering. Keep a number as state and render it after every change. If you only change visible text, later code may still read an old state value. Predict three clicks and one reset before trying the browser lab.

State changes first, display follows JavaScript
let count = 0;

function increment() {
  count += 1;
}

function reset() {
  count = 0;
}

increment();
increment();
reset();
increment();
console.log(count);
Check your prediction
Text
1

These functions simulate the same state transitions without a browser. The DOM listener can call them and then update textContent.

Your turn

Exercise

Events & State: practice

Model a reset button: start at 20, write reset() that sets minutes to zero, call it, and print 0. You will connect it to a real button later in this lesson.

Your working space JavaScript
let minutes = 20;
// Define reset, call it, then print minutes.
Hint 1: the idea

A reset is an assignment to the existing state, not another local variable.

Hint 2: a first step

Use minutes = 0 inside the function; do not write let minutes there.

Show one solution
JavaScript
let minutes = 20;

function reset() {
  minutes = 0;
}

reset();
console.log(minutes);
Text
0

In the browser version, call render after changing state. You will build that version in the webpage lab at the end of this lesson.

What you should be able to explain

  • When does a registered listener execute?
  • What is the source of truth for the count?
  • Why should reset also update the display?

Try the webpage

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

Run the page and click Add 5 twice. Find the three pieces that make the button work: the minutes state, the render function that copies state onto the page, and the addEventListener line that connects the button to addFive. Use Tab and Enter to try the button without a mouse.

Add a real Reset button

Exercise

A Reset button on the page

Turn your console reset into a working button. Add a Reset button beside Add 5 minutes. Clicking it sets the count back to zero and shows it. Check it with this sequence: Add 5, Add 5, Reset, Add 5. The page should then show:

Text
Minutes: 5

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

Hint 1: the idea

A working button needs three things, and the Add button already shows all of them: an element in the HTML, a function that changes state and then calls render, and a line that registers that function as the button's click listener.

Hint 2: a first step

Add <button id="reset" type="button">Reset</button> on the line after the Add button, outside the <script> tags, and run the page. The button appears but does nothing yet. Next, write the function inside the script.

Show one solution

Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.

reset assigns minutes = 0 (no let, so it changes the existing state instead of creating a new local variable) and then calls render, because changing a variable never updates the page on its own. Without that render() call, clicking Reset would leave "Minutes: 10" on screen even though the state is 0, and the next Add 5 would jump to 5. The listener line passes reset without parentheses: the browser calls it later, on each click.

Was this article helpful?

Your response is saved on this device.