Why does a click bubble?
The three phases of an event, where they came from, why delegation is cheap, and why stopPropagation is dangerous.
Elements are nested, so a click on a button is also a click on the list item, the list and the whole page. Who should be told first? Netscape said: top down, from the page to the button. That's capturing. Internet Explorer said the opposite: bottom up, from the button to the page. That's bubbling. The W3C standard made peace by taking both.
Today every event goes through three phases:
- capture: from
windowdown through the ancestors to the target; - target: the event on the element that was actually clicked;
- bubble: from the target back up to
window.
A plain addEventListener listens to the bubble phase, because usually you want the most specific element to react first. A third argument of true (or { capture: true }) subscribes to the capture phase.
const shop = document.querySelector("#shop");
const list = document.querySelector("#toys");
const button = document.querySelector("#buy");
shop.addEventListener("click", () => console.log("1. shop, capture"), true);
list.addEventListener("click", () => console.log("2. list, capture"), { capture: true });
button.addEventListener("click", () => console.log("3. button, target"));
list.addEventListener("click", () => console.log("4. list, bubble"));
shop.addEventListener("click", () => console.log("5. shop, bubble"));
button.click();Why is bubbling a gift? Picture a catalog of 500 toys. You could attach 500 handlers: 500 functions in memory, each of which you must remember to remove. Or one on the list: a click on any button bubbles up to it anyway, and event.target.closest(".add") tells you which one. That's delegation. Its biggest win isn't even speed: it works for elements that don't exist yet. Re-render the list, load the next page of products, and the listener is still there, because it lives on the container, not on the buttons.
const list = document.querySelector("#toys");
list.addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button) return;
console.log("Bought:", button.dataset.toy);
});
// A new toy shows up AFTER subscribing
const fresh = document.createElement("button");
fresh.dataset.toy = "laser pointer";
fresh.textContent = "Buy the pointer";
list.append(fresh);
fresh.click();addEventListener, but its click bubbled to the list, and it worked.document.addEventListener("click", () => console.log("menu closed"));
const buy = document.querySelector("#buy");
buy.addEventListener("click", (event) => {
event.stopPropagation();
console.log("mouse bought");
});
buy.click();
console.log("...and the menu is still hanging open");document never heard it.Two small things to finish:
stopPropagation()and preventDefault() are different. The first stops the event's trip through the tree, the second cancels the browser's action. A link withstopPropagation()still opens.- Not every event bubbles:
focus,blurandmouseenterdon't. For delegating focus there are bubbling twins,focusinandfocusout.
Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.