← Back to JavaScript, page 2

javascript-0030 · Medium mid

What are event bubbling and capturing in the DOM, and how does event delegation use them?

When a DOM event fires, it travels down from the window to the target (capturing), then runs on the target, then travels back up through the ancestors (bubbling). Listeners run during bubbling by default. Event delegation puts one listener on a parent and uses event.target to handle events from many children, including children added later.

Full answer

A click on a button inside a list is also a click on the list item, the list, the body, and the document. The browser sends the event along that path in three phases. In the capturing phase it goes from the outside in to the target's parent. At the target it runs the listeners on the element itself. In the bubbling phase it goes back out, from the parent to the window.

addEventListener(type, handler) registers for the bubbling phase. Passing true or { capture: true } registers for capturing. event.target is the element that started the event, and event.currentTarget is the element whose listener is running now. stopPropagation() stops the event from reaching later elements on the path, and stopImmediatePropagation() also stops other listeners on the same element. preventDefault() is unrelated: it cancels the browser's default action, such as following a link.

Event delegation uses bubbling. Instead of adding a listener to each of 500 rows, one listener is added to the table. Inside it, event.target.closest('tr') finds the row that was clicked. This uses less memory, and rows added later work without extra code. Delegation does not work for events that do not bubble, such as focus, blur, and mouseenter. Use focusin, focusout, or capture for those.

javascript
document.body.innerHTML = '<ul id="list"><li data-id="1">Notes <button>Delete</button></li></ul>';
const list = document.getElementById('list');
const log = [];

list.addEventListener('click', () => log.push('list capture'), { capture: true });
list.addEventListener('click', (event) => {
  log.push('list bubble');
  const button = event.target.closest('button');
  if (button && list.contains(button)) {
    log.push(`delete requested for item ${button.closest('li').dataset.id}`);
  }
});
document.body.addEventListener('click', () => log.push('body bubble'));

list.querySelector('button').addEventListener('click', (event) => {
  log.push('button target, currentTarget is ' + event.currentTarget.tagName);
});

list.querySelector('button').click();
console.log(log);

const newItem = document.createElement('li');
newItem.dataset.id = '2';
newItem.innerHTML = 'Report <button>Delete</button>';
list.appendChild(newItem);
log.length = 0;
newItem.querySelector('button').click();
console.log(log);

const stopper = document.createElement('div');
stopper.innerHTML = '<span></span>';
document.body.appendChild(stopper);
const hits = [];
stopper.addEventListener('click', () => hits.push('div'));
stopper.firstChild.addEventListener('click', (event) => {
  event.stopPropagation();
  hits.push('span');
});
stopper.firstChild.dispatchEvent(new Event('click', { bubbles: true }));
console.log(hits);

Written for a browser console or script tag; checked here with Node.js v24.22.1 and jsdom, two runs with identical output. Output: [ 'list capture', 'button target, currentTarget is BUTTON', 'list bubble', 'delete requested for item 1', 'body bubble' ] [ 'list capture', 'list bubble', 'delete requested for item 2', 'body bubble' ] [ 'span' ]

Key points

  • Event path: capture down, target, bubble up.
  • Listeners run in the bubbling phase unless capture is enabled.
  • target is where it started; currentTarget is where the running listener is attached.
  • Delegation: one parent listener plus closest() handles many children, including future ones.
  • focus and blur do not bubble; use focusin and focusout.

Common mistakes

  • Believing the target phase and the bubbling phase are the same thing.
  • Using event.target directly when the clicked element can be a child of the intended one, such as an icon inside a button. Use closest().
  • Calling stopPropagation everywhere, which breaks analytics and other delegated listeners.
  • Using delegation for focus or blur, which do not bubble.

Follow-ups

  • What is the difference between event.target and event.currentTarget?
  • How do passive listeners and the once option change addEventListener?
  • Which events do not bubble, and how would you delegate them?

Tips

  • Draw the three phases as an arrow going down and back up; interviewers often ask for a diagram.
  • Always mention closest() in the delegation answer, since a click often lands on a child element.
  • Name one real benefit: one listener for a long table, and dynamic rows work automatically.