Event Bubbling & Propagation
When an event happens on an element, it does not stay there. The browser first sends it down from the window to the element, which is the capture phase, then delivers it to the element itself, and then sends it back up through every ancestor to the window, which is the bubbling phase. Together these movements are called propagation.
Listeners run in the bubbling phase by default, so a click on a list item also reaches a listener on the list, on the section that holds the list and on the document. This is what makes it possible to handle clicks on many elements with a single listener on their parent.
The examples assume the script runs after the elements exist, for example from a <script> element at the end of the <body>.
Bubbling
Section titled “Bubbling”This snippet puts a listener on three nested elements and logs each one as the event reaches it.
<section id="shelf"> <ul id="books"> <li id="first-book">The Hobbit</li> </ul></section>for (const id of ['shelf', 'books', 'first-book']) { document.getElementById(id).addEventListener('click', (event) => { console.log(`${event.currentTarget.id} heard a click on ${event.target.id}`); });}Clicking the book logs three lines to the console, starting from the innermost element: first-book heard a click on first-book, then books heard a click on first-book, then shelf heard a click on first-book.
event.target stays the same all the way up, because it is the element where the event started. event.currentTarget changes, because it is the element whose listener is running.
Most events bubble, but some do not, among them focus, blur, mouseenter and mouseleave. Their bubbling equivalents are focusin, focusout, mouseover and mouseout. The bubbles property of an event says which kind it is.
Listening in the capture phase
Section titled “Listening in the capture phase”Passing { capture: true } as the third argument makes a listener run on the way down instead. Changing the loop in the previous example to use it reverses the order of the lines, so the outermost element hears the click first.
for (const id of ['shelf', 'books', 'first-book']) { document.getElementById(id).addEventListener('click', (event) => { console.log(`${event.currentTarget.id} heard a click on ${event.target.id}`); }, { capture: true });}With the same HTML, clicking the book now logs shelf first, then books, then first-book. Older code passes true as the third argument, which means the same thing. Capture listeners are rarely needed, but they let a parent see an event before any child can stop it.
Stopping propagation
Section titled “Stopping propagation”event.stopPropagation() stops the event from travelling any further, so listeners on the elements beyond the current one do not run. event.stopImmediatePropagation() does the same and also skips any other listeners for the same event on the current element.
<div id="card"> <h2>Wireless headphones</h2> <button id="favourite">Add to favourites</button></div>document.getElementById('card').addEventListener('click', () => { console.log('Opening the product page');});
document.getElementById('favourite').addEventListener('click', (event) => { event.stopPropagation(); console.log('Added to favourites');});Clicking anywhere on the card logs Opening the product page, while clicking the button logs only Added to favourites, because the button’s listener stops the event before it reaches the card.
Use it sparingly. A stopped event never reaches listeners higher up, including ones added later by other code, such as one on the document that closes an open menu when the user clicks elsewhere. stopPropagation() also does not cancel the browser’s default action, which is what preventDefault() does.
Event delegation
Section titled “Event delegation”Because events bubble, one listener on a parent can handle events for all its children, including children added after the listener was attached. This is called event delegation. The listener uses event.target to work out which child was clicked, and closest() finds the nearest matching element, which copes with a click that lands on something inside the button.
<ul id="todo-list"> <li>Water the plants <button data-action="done">Done</button></li> <li>Post the parcel <button data-action="done">Done</button></li></ul><button id="add-todo">Add a task</button>const list = document.getElementById('todo-list');
list.addEventListener('click', (event) => { const button = event.target.closest('button[data-action="done"]'); if (!button) return; button.closest('li').remove();});
document.getElementById('add-todo').addEventListener('click', () => { const item = document.createElement('li'); item.innerHTML = 'New task <button data-action="done">Done</button>'; list.append(item);});Clicking Done removes that task, and the buttons on tasks added later work too, because the single listener sits on the list rather than on each button. Clicks on the task text pass the if check without doing anything.
Steve Griffith covers this topic in his video JavaScript Event Bubbling and Propagation.