Skip to curriculum
Jump to the editor

17.8. Event delegation for a list

Easy
Topics
10–20 min

An inbox shows hundreds of messages, each with a Delete button, and new messages arrive while the page is open. Adding a listener to every button costs memory, and every new row needs its own listener. Event delegation uses one listener on the list instead: a click on any button bubbles up to the list, and the listener works out which item it came from.

Write delegate(root, type, selector, handler) in delegate.js.

Signature

JAVASCRIPT
function delegate(root, type, selector, handler) // returns a function
  • root: the element that gets the one listener (the list).
  • type: an event type that bubbles, such as "click", "input" or "keydown".
  • selector: a CSS selector for the items, such as ".message" or "button.delete".
  • handler: called as handler.call(matched, event, matched).
  • Returns a function that removes the listener.

Requirements

  1. delegate adds one listener, on root, and none on the items.
  2. When an event of type reaches root, the handler runs if the event's target, or one of its ancestors below root, matches selector.
  3. The handler gets the event and the matched element, and this is the matched element.
  4. Only the nearest match to the target is used, and the handler runs at most once per event.
  5. root itself and elements above it never count as a match, even if they match selector.
  6. Items added after delegate was called work with no extra code.
  7. A text node as the target is matched from its parent element.
  8. Events of other types are ignored.
  9. The listener runs in the bubbling phase: after the item's own listeners, and not at all when one of them stops propagation.
  10. The returned function removes this listener (calling it twice is harmless) and leaves other delegations alone.

Examples

JAVASCRIPT
// <ul id="inbox"><li class="message" data-id="1"><b>Ada</b> … <button class="delete">Delete</button></li> …</ul>
const stop = delegate(inbox, "click", ".message", function (event, item) {
  console.log("open", item.dataset.id, this === item);
});

// click on <b>Ada</b> inside message 1   → "open 1 true"
// click between two messages, on the ul  → nothing
JAVASCRIPT
delegate(inbox, "click", "li, button", (event, matched) => console.log(matched.tagName));
// click on a Delete button → "BUTTON" once, not "BUTTON" and then "LI"
JAVASCRIPT
stop();  // later clicks on messages log nothing

Environment

  • Plain JavaScript in a browser page; the tests call the global delegate on lists they build and dispatch real bubbling events (click, dblclick, input, keydown).
  • Events that do not bubble (focus, blur, mouseenter) are out of scope; their bubbling partners are focusin, focusout and mouseover.
  • Element.closest and Element.matches are fine to use.

Hints

  1. event.target is the innermost element. Which method walks up from it looking for a selector?
  2. That method does not stop at root. What do you need to check about what it returns?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…