FrontendIntermediate
Event Bubbling
Pronunciationi-VENT BUB-ling
Definition
Event Bubbling is a mechanism in the DOM where an event triggered on a nested element propagates upward through its parent elements. This allows a single event listener on a container to handle events triggered by its children.
Where you hear it
Commonly discussed when managing event listeners in JavaScript, debugging unexpected event triggers, or implementing event delegation patterns.
Examples
Clicking a button inside a div triggers the click event on the button first, then the div.
You can use event delegation to attach one listener to a list instead of adding listeners to every list item.
The click on the icon bubbles up to the card, which then opens the details.
Common mistake
Developers often forget that events bubble up by default and may accidentally trigger multiple handlers, which can be prevented using event.stopPropagation().
Don't confuse with
Event Bubbling goes upward from the target to the parents, while Event Capturing goes downward from the root to the target element.
Say it at work
- In a meeting or chat
Make sure to call event.stopPropagation here, otherwise event bubbling will trigger the parent container as well.
- In an email or ticket
We can refactor this component to rely on event bubbling instead of attaching separate listeners to every single child.