Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
17.8. Event delegation for a list
Easy
Topics
This question is in
17. DOM: changing the page and eventsAn 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
function delegate(root, type, selector, handler) // returns a functionroot: 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 ashandler.call(matched, event, matched).- Returns a function that removes the listener.
Requirements
delegateadds one listener, onroot, and none on the items.- When an event of
typereachesroot, the handler runs if the event's target, or one of its ancestors belowroot, matchesselector. - The handler gets the event and the matched element, and
thisis the matched element. - Only the nearest match to the target is used, and the handler runs at most once per event.
rootitself and elements above it never count as a match, even if they matchselector.- Items added after
delegatewas called work with no extra code. - A text node as the target is matched from its parent element.
- Events of other types are ignored.
- The listener runs in the bubbling phase: after the item's own listeners, and not at all when one of them stops propagation.
- The returned function removes this listener (calling it twice is harmless) and leaves other delegations alone.
Examples
// <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 → nothingdelegate(inbox, "click", "li, button", (event, matched) => console.log(matched.tagName));
// click on a Delete button → "BUTTON" once, not "BUTTON" and then "LI"stop(); // later clicks on messages log nothingEnvironment
- Plain JavaScript in a browser page; the tests call the global
delegateon 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 arefocusin,focusoutandmouseover. Element.closestandElement.matchesare fine to use.
Hints
event.targetis the innermost element. Which method walks up from it looking for a selector?- 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…