JavaScript DOM Events Interview Questions: Practice Quiz

Reviewed by Mark Dickie · Last updated

4 questions on this page you can answer and see marked on the spot. Go to the first one

DOM events are the signals a browser fires when something happens to an element, such as a click or a form submit. For a JavaScript interview, you need to know how a listener is attached, how an event travels through the capture and bubble phases, and how to stop or reuse that flow. Most questions ask why a handler ran on the wrong element and how to fix it. Expect to reason about event.target versus event.currentTarget, and about when delegation beats attaching a listener to every child.

What do interviewers check on DOM events?

TopicWhat to be able to explainTypical trap
Event phasesCapture runs from window down to the target, then bubble runs back up. addEventListener defaults to bubble unless you pass { capture: true }Assuming capture listeners on a parent always run after bubble listeners on a child
event.target vs event.currentTargettarget is the element that fired the event; currentTarget is the element the listener is attached toReading target in a delegated handler and getting a child <span> instead of the <li>
stopPropagation vs stopImmediatePropagationThe first stops the event reaching ancestors; the second also blocks other listeners on the same nodeExpecting stopPropagation to cancel a form submit, which is preventDefault's job
preventDefaultCancels the default action such as link navigation or form submission, and only works on cancelable eventsCalling it inside a { passive: true } listener, where the browser ignores it
once, passive, signal optionsonce removes the listener after the first call; an AbortController signal removes it on abort()Passing a new anonymous function to removeEventListener, which never matches the original
focusin / focusout vs focus / blurfocus and blur do not bubble; focusin and focusout doA bubble-phase focus listener on a parent container never fires for child elements. Use focusin, or pass { capture: true }.

A few hands-on exercises that show up in live coding rounds:

  1. Build a delegated click handler on a <ul> that toggles a class on whichever <li> was clicked, including items added after page load.
  2. Log the order of capture and bubble listeners on three nested divs, predict the output, then run it to check.
  3. Attach a listener with an AbortController signal, call abort(), and confirm a second click no longer fires.
  4. Write a form submit handler that calls preventDefault only when validation fails, so valid input submits normally.

Key facts

  • Tarmac has 11 JavaScript interview questions on this topic, 10 of them on this page, at difficulty 1–5 of 5.
  • Tarmac tracked 1,180 job postings asking for JavaScript in November 2026.
  • Roles asking for JavaScript advertise a median base salary of £72,250, across 456 job postings as of November 2026.
  • Tarmac last reviewed these JavaScript interview questions on 3 December 2026.

At a glance

Questions10 shown · 11 in the bank
Difficulty1–5 of 5
FormatsMultiple answer, Ordering, True / false, Code output, Short answer, Find the bug, Multiple choice, Fill in the blank, Flashcard, Coding exercise
Interactive1 run your code against tests, in the app

What you'll review

  1. event delegation

Practice questions

Try one before you open the answer. Pick an option and press Check; it's marked on the spot.

You attach one click listener to a <ul> instead of attaching one to each <li> (event delegation). Which statements about this approach are true? Select all that apply.#

Options

Pick every one that applies.

Show answer

Event delegation with one listener on a <ul> works because clicks bubble up from each <li> to the list. <li> items added later are handled automatically, and event.target shows which element was clicked. event.currentTarget is the <ul> that owns the listener, not the <li>. You don't need { capture: true }, because a normal bubbling listener is enough.

Why:

Event delegation puts a single listener on an ancestor and depends on events bubbling up from descendants, so elements added later are covered automatically. Inside the handler, event.target is the element where the event started, which may be the <li> or a child inside it. event.currentTarget is the element that owns the listener, which here is the <ul>, so the claim that it is the clicked <li> is false. Capture is not needed, because the default bubble-phase listener works for bubbling events like click.

A <ul id="list"> has one delegated listener: list.addEventListener('click', handler), registered with default options (bubbling phase). A user clicks a <button> inside one of its <li> items. Put the steps in the order they happen.#

Put these in order

Show answer

In event delegation, a click first travels down from window in the capture phase. Next it reaches the clicked button in the target phase. Then it bubbles up to the parent <ul>, where the delegated listener runs. Finally the handler checks event.target.closest('li') to see which child was clicked. Delegation works because events bubble up to ancestors.

Why:

DOM events go through three phases. First, the capture phase runs from window down to the target's parent. Then comes the target phase at the clicked button. Last, the event bubbles back up through the ancestors. The listener on the <ul> was added without capture: true, so it runs while the event bubbles past the <ul>. Only after the handler has been called can its code check event.target (for example with closest('li')) to find out which child started the event. This is the basic pattern of event delegation.

With a single delegated click listener on a <ul>, list items (<li>) that are appended to the list after the listener was attached will also trigger that listener when clicked, with no additional addEventListener calls.#

Options

Show answer

Yes. A delegated click listener on a <ul> also handles clicks on <li> items added later. Click events bubble from the clicked item up to its parent list, so one listener on the parent catches clicks from every current and future child. The handler finds the clicked item with event.target or event.target.closest('li').

Why:

Event delegation works because click bubbles. A click on any <li> travels up through its ancestors, and when it reaches the <ul>, the listener attached there runs. The listener is registered on the parent rather than on each child, so it doesn't matter when a child was added. Items appended later still bubble their clicks to the same <ul>, and the handler can tell which item was clicked from event.target, or from event.target.closest('li') if the click landed on an element inside the item.

Given this HTML in a browser page:#

const list = document.getElementById('list');
list.addEventListener('click', (e) => {
  const item = e.target.closest('li');
  console.log(e.target.tagName, e.currentTarget.tagName, item.dataset.id);
});
Show answer
SPAN UL 2
Why:

The click starts on the innermost element, so e.target is the <span> and its tagName is SPAN (HTML tag names come back in uppercase). The event bubbles up to the <ul>, where the delegated listener is attached, so e.currentTarget is the <ul> and logs UL. closest('li') starts at the span and walks up to the nearest <li> ancestor, which has data-id="2", so dataset.id is the string 2. This is why delegated handlers use closest(): the element that was clicked is often a child of the item you actually want.

A delegated click handler is attached to a <ul id="list">:#

Show answer

e.target is the innermost element actually clicked, so clicking the <span> or <img> makes e.target that child, which doesn't match li.item, and the check fails. Use closest() to walk up from the target to the nearest matching ancestor, and make sure it belongs to the list: const li = e.target.closest('li.item'); if (li && list.contains(li)) remove(li.dataset.id);. (e.currentTarget would be the <ul> itself, so it's not the fix.)

Why:

In event delegation the listener sits on the ancestor, but event.target points to the deepest element that started the event. Bubbling doesn't change target. When the user clicks a nested <span> or <img>, the matches('li.item') test fails. Element.closest(selector) checks the element itself and then each ancestor in turn, so it finds the containing <li> from any descendant. The list.contains(li) check (or li being inside currentTarget) stops it from matching an li.item that is an ancestor of the list. currentTarget is always the element with the listener, which here is the <ul>, so it can't identify the item.

Each todo is rendered as <li data-id="42"><span class="label">Buy milk</span> <button class="del">×</button></li> inside <ul id="todo-list">. A delegated listener is meant to remove a todo when the user clicks anywhere on its row. In testing, clicking the label text or the × button calls removeTodo(undefined). The function only works when the user clicks the bare padding of the <li>. Which line is buggy?#

const list = document.querySelector('#todo-list');
list.addEventListener('click', (e) => {
  const item = e.target;
  if (!item || !list.contains(item)) return;
  const id = item.dataset.id;
  removeTodo(id);
});
Show answer

The bug is on line 3.

Why:

The bug is on line 3. e.target is the innermost element that was actually clicked, so it can be the <span> or the <button>, not the <li>. Neither of those has a data-id attribute, so item.dataset.id comes back undefined. In a delegated handler you have to walk up from the target to the element you care about: const item = e.target.closest('li[data-id]');. The list.contains(item) guard on line 4 then makes sure the matched ancestor is inside this list. Line 4 isn't the problem, because the span and button really are inside the list, so the check passes. Lines 5 and 6 are only wrong because they receive the wrong element from line 3.

You add one delegated listener on a form — form.addEventListener('focus', handler) — to detect when any of its inputs gains focus, but handler never fires when an input is focused. Why?#

Options

Show answer

Because focus does not bubble, and delegation relies on the event bubbling up to the ancestor listener. Most events bubble from the target through their ancestors, but focus and blur are exceptions, so a bubbling-phase listener on the form never sees them. Use the bubbling counterpart focusin, or listen in the capture phase with { capture: true }.

Why:

Event delegation works because most events bubble from the target up through its ancestors, where one shared listener can catch them. focus and blur are exceptions that do not bubble, so a bubbling-phase listener on the form never sees them — the fix is the bubbling counterparts focusin/focusout, or registering the listener with { capture: true } since the capture phase still passes through ancestors.

A delegated handler on a <ul id="menu"> must react to clicks on any <li class="item">, even when the user clicks a nested <span> or <img> inside the item. It must also ignore matching <li> elements that sit outside the menu (for example, an ancestor of the <ul>).#

Show answer

A delegated handler on a <ul id="menu"> must react to clicks on any <li class="item">, even when the user clicks a nested <span> or <img> inside the item. It must also ignore matching <li> elements that sit outside the menu (for example, an ancestor of the <ul>).

menu.addEventListener('click', (e) => {
  const li = e.target.**closest**('li.item');
  if (!li || !menu.**contains**(li)) return;
  console.log(li.dataset.id);
});

Fill in the two DOM method names.

Why:

e.target is the innermost element clicked, which may be a <span> or <img> inside the <li>, so checking e.target.matches('li.item') would miss those clicks. Element.closest(selector) walks up from the target, including the target itself, and returns the first ancestor that matches. Because closest keeps climbing past the listener's element, it can return an li.item that is an ancestor of the <ul>. Checking menu.contains(li) rules that out, so the handler only responds to items inside the delegating container. This is the standard pattern for robust event delegation.

A delegated click handler on document uses e.target.closest('button[data-action]'). It works for light-DOM buttons but returns null for matching buttons rendered inside a custom element's open shadow root. Why, and how do you fix it?#

Show answer

Shadow DOM retargeting: when the event is observed outside the shadow tree, e.target is re-set to the shadow host, so closest() walks the host's light-DOM ancestors and never sees the inner button. Fix: use e.composedPath(), which lists every node in the propagation path, including nodes inside open shadow roots, and find the first element that matches, e.g. e.composedPath().find(n => n instanceof Element && n.matches('button[data-action]')). Caveats: this only works for composed: true events (native UI events like click are composed). Nodes inside a closed shadow root are left out of the path seen by outside listeners. Calling composedPath() after dispatch has finished returns an empty array, so call it synchronously.

Why:

Encapsulation retargets event.target across each shadow boundary, so delegation that relies on target/closest can't see inside components. composedPath() exposes the real path, except for closed roots and non-composed events.

Write a delegated event dispatcher that works without a real DOM and follows the propagation rules libraries such as jQuery use for delegated handlers.#

Starter code

function delegate(nodes, containerId, targetId, handlers) {
  // TODO: build path target->container (exclusive), dispatch with stop semantics
  return [];
}

Your solution must pass

  • full bubbling through matching ancestors
  • stop finishes current node

This one is written and run, not read. Solve it in the app and your code is executed against these tests and the hidden ones.

Sources

The official documentation these questions are checked against:

Related interview questions

Job market

See javascript salaries and hiring demand from live job postings.

The other 1 question

This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 1 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.

Start with this topic

Free · the whole bank · 100 answers per 30 days, each marked with written feedback

Interview booked? Practise the job ad instead

What moved, monthly

One email a month when the bulletin comes out: what moved in the markets we track, and the new question topics we published. Confirm your address to join. Unsubscribe any time.