Frontend interview practice question

Why does my click handler fire twice? Bubbling, capturing, and event delegation

HighIntermediateJavascript

Interview quick answer

Debug nested click handlers the way interviewers probe them: the capture, target, and bubble phases, event.target vs event.currentTarget, why stopPropagation hides the real bug, and how one delegated listener with closest() handles dynamic lists safely.

Interview focus

This JavaScript interview question tests whether you can explain Click Handler Fires Twice: Bubbling and Delegation, connect it to production trade-offs, and handle common follow-up questions.

  • Click Handler Fires Twice: Bubbling and Delegation explanation without falling back to memorized definitions
  • Event Delegation and DOM reasoning, edge cases, and production failure modes
  • How you would answer the most likely JavaScript interview follow-up
Practice more JavaScript interview questions
Interview answer drill

Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Full interview answer

The bug to explain

A save button inside a modal panel inside a backdrop: one click, three handlers run. Nothing is duplicated. The event travels a three-phase path, capture -> target -> bubble, and every ancestor with a listener for that phase gets its turn. That same propagation is what makes event delegation possible: one listener on a stable ancestor handles every child, including children added later. The interviewer wants both halves: why the parent fires, and how to read the real target without reaching for stopPropagation() first.

Phase

Direction

Triggered On

Listener Option

Capturing

Top → Down

From document to target

addEventListener('click', handler, true)

Target

—

Actual element clicked

Event fires directly on target

Bubbling

Bottom → Up

From target to document

addEventListener('click', handler) (default)

The three phases of DOM event propagation.
HTML
<div id='backdrop'>
  <div id='panel'>
    <button id='save'>Save</button>
  </div>
</div>
                  
JAVASCRIPT
backdrop.addEventListener('click', (event) => {
  console.log('backdrop', {
    target: event.target.id,
    currentTarget: event.currentTarget.id
  });
});

panel.addEventListener('click', (event) => {
  console.log('panel', {
    target: event.target.id,
    currentTarget: event.currentTarget.id
  });
});

save.addEventListener('click', (event) => {
  console.log('button', {
    target: event.target.id,
    currentTarget: event.currentTarget.id
  });
});
                  

Output when you click the save button:

button { target: 'save', currentTarget: 'save' }
panel { target: 'save', currentTarget: 'panel' }
backdrop { target: 'save', currentTarget: 'backdrop' }

This is the first follow-up most people miss: event.target is the deepest clicked node, while event.currentTarget is whichever listener is currently running.

Single trace: capture -> target -> bubble

A combined trace is often the fastest way to debug whether the wrong handler is firing or the right handler is firing in the wrong phase.

JAVASCRIPT
function trace(label) {
  return (event) => {
    console.log(label, {
      target: event.target.id,
      currentTarget: event.currentTarget.id
    });
  };
}

shell.addEventListener('click', trace('shell capture'), true);
menu.addEventListener('click', trace('menu capture'), true);
item.addEventListener('click', trace('item target'));
menu.addEventListener('click', trace('menu bubble'));
shell.addEventListener('click', trace('shell bubble'));
                  

Expected order for a click on item:

shell capture -> menu capture -> item target -> menu bubble -> shell bubble

This makes phase order visible and shows why target stays the same while currentTarget changes as the event moves.

The Core Idea

Event delegation lets you attach one event listener to a common ancestor instead of wiring every child separately. The production/debug value is not just fewer listeners. It is surviving dynamic DOM updates, nested click targets, and propagation edge cases without silently handling the wrong element.

JAVASCRIPT
// Example: Without delegation
const items = document.querySelectorAll('li');
items.forEach(item => item.addEventListener('click', () => {
  console.log('Clicked:', item.textContent);
}));
                  
JAVASCRIPT
// Example: With event delegation
const list = document.querySelector('ul');
list.addEventListener('click', (event) => {
  const item = event.target.closest('li[data-id]');
  if (!item || !list.contains(item)) return;
  console.log('Clicked item:', item.dataset.id);
});
                  

Concept

Explanation

Example / Note

Event Bubbling

Events start from the target element and bubble up to its ancestors.

click on li → bubbles to ul

event.target

The original element that triggered the event.

Used to identify which child was clicked

event.currentTarget

The element that the event listener is attached to.

Usually the parent in delegation

How event delegation uses bubbling and target references.

Why Use Event Delegation?

  • Better performance — fewer event listeners in the DOM.
  • Handles dynamic elements added later (since the listener is on a stable ancestor).
  • Cleaner, more maintainable code for lists, tables, or repeated components.
JAVASCRIPT
// Example: Dynamic content
const container = document.querySelector('#buttons');

container.addEventListener('click', (e) => {
  if (e.target.matches('button.delete')) {
    e.target.remove();
  }
});

// Works even if buttons are added later!
                  

Production debugging example
In a comment list, a click may land on an inner svg or span, not on the button you visually clicked. Guard with closest() and confirm the matched element still belongs to the delegated container.

JAVASCRIPT
list.addEventListener('click', (event) => {
  const action = event.target.closest('button[data-action]');
  if (!action || !list.contains(action)) return;

  if (action.dataset.action === 'delete') {
    removeComment(action.closest('li[data-id]'));
  }
});
                  

Follow-up confusion to answer on the page

  • Delegation depends on propagation; if a child calls stopPropagation(), the parent listener may never run.
  • Not every event is an ideal delegation candidate. focus/blur behave differently from click.
  • Keyboard interactions may need separate handling so delegation stays accessible.

Common Pitfalls

  • Forgetting to check event.target → all clicks bubble and trigger the handler.
  • Relying on event.target when nested elements are inside clickable items (use .closest() to handle this safely).
JAVASCRIPT
// Example: Using closest() for nested targets
list.addEventListener('click', (event) => {
  const li = event.target.closest('li');
  if (!li) return; // click outside any li
  console.log('Clicked item:', li.textContent);
});
                  

Likely follow-up confusion

  • event.target answers 'what was actually clicked?'
  • event.currentTarget answers 'which listener is running right now?'
  • Capture is opt-in; bubbling is default.
  • Use stopPropagation() narrowly, because it can break delegation, analytics, and outside-click handlers higher in the tree.
  • If a component uses shadow DOM, composedPath() can reveal the full path more accurately than guessing from one node.

Stopping propagation

You can stop the event from moving further in either direction using:

  • event.stopPropagation() -> stops it from continuing up or down.
  • event.stopImmediatePropagation() -> also prevents other handlers on the same element from firing.

Use it as a targeted fix, not as a blanket answer to every propagation bug.

JAVASCRIPT
document.getElementById('save').addEventListener('click', (event) => {
  event.stopPropagation();
  console.log('Only the save button handles this.');
});
                  

Practical scenario
A long, dynamic list of comments can be handled with a single click listener on the list container.

Common pitfalls

  • Using event.target directly instead of closest() to find the intended item.
  • Forgetting that stopPropagation can block delegation.
  • Not handling keyboard events, so accessibility suffers.
Trade-off or test tip
Delegation saves memory but adds selector logic. Test by adding/removing items dynamically and ensuring handlers still work.

Still so complicated?

Imagine a restaurant. Instead of each waiter (child) taking orders, there’s one manager (parent) who hears all requests and decides who called — that’s event delegation!

Summary

  • Attach listener on parent, not each child.
  • Use event bubbling + event.target (or closest()) to detect the real source.
  • Improves performance and works for dynamically added elements.

Guides
Preparing for interviews?

Built in public

The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.

Star on GitHub