Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow a Click Up the Page

Late guests’ rows join the list after drawPlayers() has run its loop, so their buttons would get no listener. In this video, you gave a listener its event parameter and followed a click on Wei’s Check in button up through the page, to see whether one listener higher up could hear every row. Here are the key points:

  • The browser calls every listener with one argument, an event object, which describes what happened. Give the arrow a parameter, named event by most code, to use it. The object holds more than the properties below, but they’re all you need for now. Once you delete the log lines, you can leave the parameter in place. Unused, it does no harm.
  • event.type is the kind of event, click here.
  • event.target is the element the click landed on. In the Check in button’s own listener, that’s Wei’s button.
  • A click travels up the page’s tree. It starts at the element it lands on, then moves through every element that element sits inside: Wei’s list item, the players list, main, body, html and the document. This trip is called bubbling.
  • Every click listener on the way runs, from the button outward. A click on Wei’s Check in button logs the button’s own two lines, then the list’s line, then the document’s.
  • event.currentTarget is the element whose listener is running. The target stays the same all the way up, and the current target changes at each listener on the way. In the list’s listener, the target is still Wei’s button, and the current target is the list, whose id is players. The code logs the list’s id because its text would be every row at once.
  • With a mouse, the target is the innermost element under the pointer, which isn’t always a button. A click on Sam’s name makes the span that holds it the target. Sam’s Check in listener doesn’t run, because the button isn’t above the span in the tree. Press Enter or Space on a focused button, and the target is the button.
  • Browsers also make an earlier pass down the tree, called capture. Listeners run on it only if you opt in. That’s all you need about capture for now.

At the end of main.js, below everything else, add the two listeners. playerList is already declared near the top of the file:

Language: JavaScript
playerList.addEventListener("click", (event) => {
  console.log("Target:", event.target.textContent);
  console.log("Current target:", event.currentTarget.id);
});
document.addEventListener("click", () => {
  console.log("Document heard a click");
});

Save, reload, and click Wei’s Check in button. Below the lines that section 1’s experiments log as the page loads, the Console shows the button’s own two lines, then the list’s two lines, then the document’s line:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Right now, drawPlayers gives every Check in button its own listener, made inside the loop. Four rows means four listeners.

00:09 Later in this section, guests who turn up late join the list. Their rows arrive after that loop has run, so their buttons would get no listener. Could one listener on the whole list serve every row instead?

00:22 To find out, you’ll see what the browser hands each listener, and where a click goes after it lands.

00:29 Until now, your Check in arrow took no parameters. But the browser always calls a listener with one argument, an event object that describes what happened.

00:39 Give the arrow a parameter named event, the name most code uses. At the top of its body, add two log lines for two of the object’s properties. Save, reload, and click Wei’s Check in button.

00:52 The type is click, the kind of event. The target is the element the click landed on, Wei’s button. The event object holds more than these two, but they’re all you need for now.

01:05 Delete the two log lines. You can leave the parameter in place. Unused, it does no harm.

01:12 Now listen higher up. At the end of main.js, give the players list, already in playerList, a click listener that logs that the list heard a click.

01:22 Then give document, which stands for the whole page, a listener that logs that the document heard one. Your turn: pause the video and predict which of the three listeners log a line when you click Wei’s Check in button.

01:40 Save, reload, and click. All three do. The button’s own lines come first. Then the list’s line, and last, the document’s line. They ran from the button outward.

01:54 The click starts at the button. Then it travels up the page’s tree, through Wei’s list item, the players list, main, body and html, to the document. At each element on the way, the browser runs that element’s click listeners.

02:10 This trip up the tree is called bubbling.

02:14 Browsers also make an earlier pass down the tree, called capture. Listeners run on it only if you opt in. That’s all you need about capture for now.

02:25 The list’s listener ran for Wei’s button. But which element was clicked? The event object can tell the list. It also has a current target, the element whose listener is running, which is the list.

02:37 Give the list’s arrow the event parameter, and replace its log line with two. The first logs the target’s text. The second logs the current target’s id, since the list’s text would be every row at once.

02:50 Reload, and click Wei’s button once more. The target’s text is still Check in Wei. The current target’s id is players, the list. The target stays the same all the way up.

03:03 The current target changes at each listener on the way. Now reload and click Sam’s name, not the button beside it. Your turn: pause the video and predict what the target line shows.

03:21 The target is Sam, the span that holds the name.

03:26 With a mouse, the target is the innermost element under the pointer, and it isn’t always a button. Sam’s Check in listener didn’t run, because the button isn’t above the span in the tree.

03:36 Press Enter or Space on a focused button, and the target is the button.

03:42 As a rule, a click starts at the element it lands on, then bubbles up through every element it sits inside. Each click listener on the way runs. Inside each one, the target is where the click started, and the current target is whose listener is running.

03:59 So a listener on the list hears a click on any row, and the target says where the click landed. Next, in Handle a Whole List With One Listener, you’ll get from the target to its row, and replace the four Check in listeners with one.

Become a Member to join the conversation.