Pass true as the third argument to addEventListener: element.addEventListener('click', handler, true). By default (false), listeners run in the bubbling phase.
What Is Event Bubbling in JavaScript?
How do you listen in the capturing phase in JavaScript?
The answer
More FAQs in What Is Event Bubbling in JavaScript?
When an event on a child element propagates up to its parents. A click on a button inside a div triggers the button's listener, then the div's listener, up to the document.
Call e.stopPropagation() in the event handler. This prevents the event from propagating to parent elements.
Capturing (top-down: document to target's parent), Target (event reaches the target), and Bubbling (bottom-up: target to document). Default listeners run in the bubbling phase.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.