What is event bubbling?
Short answer
Event bubbling means an event fired on an element propagates upward through its ancestors in the DOM tree, triggering any matching event listeners on parents after the target element's own listeners run.
Advertisement
AModel answer
Event bubbling means an event fired on an element propagates upward through its ancestors in the DOM tree, triggering any matching event listeners on parents after the target element's own listeners run.
document.getElementById('parent').addEventListener('click', () => console.log('parent clicked'));
document.getElementById('child').addEventListener('click', () => console.log('child clicked'));
// clicking child logs 'child clicked' then 'parent clicked'Real-world example
A single click listener on a <ul> catching clicks from any <li> inside it, instead of attaching a listener to every item.
Likely follow-ups
- How do you stop an event from bubbling further?
Related topics