Event Bubbling
Bubbling is the phase where an event travels from the target node up through its ancestors to document and window. A listener on a parent can see a click that happened on a child.
Detailed Explanation
After the browser finds the target (the deepest node that was hit), most events take a trip up the ancestor chain. A click on a <span> inside a <button> inside a <form> will run listeners on the span, then the button, then the form, then body, html, document, window — if those listeners are registered in the bubble phase (the default).
This is why a parent can handle many children with one listener (delegation). It is also why a child click "unexpectedly" runs a parent handler. event.stopPropagation() cuts the trip. Use it sparingly; it breaks delegation and analytics that listen higher up.
event.target stays the origin node during the whole bubble. event.currentTarget is the node whose listener is running now. That pair is how a list handler knows which <li> was clicked: event.target.closest("li").
Not every event bubbles. focus and blur do not (use focusin / focusout). load on images does not. scroll on document has special rules; scroll on an element generally does not bubble. Check MDN for the event you are using. bubbles: true on CustomEvent is opt-in.
event.eventPhase is Event.BUBBLING_PHASE during this trip. Capture comes first; see event capturing.
What it costs you: accidental parent handlers and over-use of stopPropagation that makes a feature "sometimes not fire" depending on where you clicked. Prefer checking event.target over stopping the event.
Reach for bubbling when a parent should react to child activity. Reach for capture when you must run before the target (a modal "first click outside"). Reach for once plus a child handler when the parent should not be involved.
Code Examples
document.querySelector("form").addEventListener("click", (event) => {
console.log("form saw", event.target.tagName, "phase", event.eventPhase);
});
document.querySelector("button").addEventListener("click", (event) => {
console.log("button");
});
// click the button: button then form saw BUTTONlist.addEventListener("click", (event) => {
const row = event.target.closest("li");
if (!row || !list.contains(row)) return;
console.log(row.dataset.id);
});child.addEventListener("click", (event) => {
event.stopPropagation(); // parent bubble listeners will not run
});Common Gotchas
`stopPropagation` breaks parent features
delegation, outside-click, and logging all sit above you.
Not all events bubble
`focus` will not reach a parent; `focusin` will.
`target` vs `currentTarget`
use `target` (or `closest`) for "what was clicked", `currentTarget` for "whose listener is this".