Skip to the solution
MediumFrontend5 min read

What is event delegation?

814 views

The problem

Question presented to candidate: "What is event delegation, in your own words, and what specific browser mechanism makes it actually possible?"

What a strong answer should cover:

  • πŸ“Œ Interview term: event delegation β€” a pattern that exploits event bubbling: instead of attaching a listener to every individual element you care about, you attach one listener to a shared ancestor and inspect event.target to determine which specific descendant triggered it.
  • πŸ“Œ Interview term: the enabling mechanism β€” event delegation is only possible because most DOM events genuinely bubble β€” travel from the actual target element up through each ancestor in turn, all the way to document. Verified directly with a real dispatched event: a listener on a distant ancestor genuinely receives a click that originated on a deeply nested descendant.
  • A precise answer distinguishes delegation from simply "having a listener that handles multiple things" β€” the defining trait is that the listener is attached to an ancestor, not to the elements themselves, and relies specifically on bubbling to reach it.
  • πŸ“Œ Interview term: not every event bubbles β€” a precise answer names that focus/blur do not bubble (their bubbling equivalents focusin/focusout do), which is a real, direct constraint on which events delegation can be used for at all.
  • A precise answer names the concrete, measured trade-off verified directly: a 1000-element collection needs exactly 1 delegated listener versus 1000 individual ones β€” the real reason this pattern exists, beyond convenience.

Clarifying questions expected:

  • None β€” this is a definitional question; naming the exact underlying mechanism (bubbling) rather than just describing the observable pattern is the strong signal.

Code / implementation expected: Optional β€” a short delegated-listener snippet demonstrating event.target usage reinforces the definition concretely.

  • event-delegation

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a listener on a distant ancestor receives a deeply nested click purely through bubbling

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 64 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the difference between call, apply, and bind?

Continue