Browser and Events17% of the exam
Events, listeners and propagation
How addEventListener registers, deduplicates and removes listeners, how an event travels through the capture, target and bubble phases, and how to choose between stopPropagation, stopImmediatePropagation and preventDefault, delegate events to a parent, and dispatch custom events with detail and bubbles.
Given a business requirement, utilize Events, event handlers, and propagation.
~8 min read · 14 practice questions ·
What the exam expects
- Register, deduplicate and remove listeners with
addEventListener, its options (once,capture,passive) andremoveEventListener, and predict what anonclickproperty does alongside them. - Trace the order in which capture, target and bubble listeners run for one click.
- Pick
stopPropagation,stopImmediatePropagationorpreventDefaultfrom the requirement. - Implement event delegation with
event.target,event.currentTargetandclosest(), knowing which events bubble. - Dispatch custom events with
detailandbubblesand predict when their listeners run. - Key terms: listener (a function registered for an event type on a target); propagation (the path an event takes through the target's ancestors); capture phase (window down to the target's parent); bubble phase (the target's parent back up to window); default action (what the browser does on its own, such as submitting a form); delegation (one listener on a parent handling events from its descendants).
Key facts
Same listener added twice
- Detail
- Same function reference, type and capture flag: added once
onclick assigned twice- Detail
- The second assignment overwrites the first
Default phase
- Detail
- Bubble; capture needs
{ capture: true }ortrue
once: true- Detail
- Listener is removed automatically after its first call
passive: true- Detail
- A
preventDefault()call inside the listener has no effect
removeEventListener match- Detail
- Type, same function reference and capture flag; otherwise no effect
stopPropagation()- Detail
- Stops ancestors; same-element listeners and default action still happen
stopImmediatePropagation()- Detail
- No remaining listener runs on this or any other element
target vs currentTarget- Detail
- Deepest element fired on vs element whose listener is running
Non-bubbling events
- Detail
focusandmouseenter; usefocusinandmouseoverto delegate
Event/CustomEvent defaults- Detail
bubbles: false,cancelable: false,detail: null
dispatchEvent() timing- Detail
- Listeners run synchronously before it returns
Sources for this section (11)
- addEventListener()developer.mozilla.org (opens in a new tab)
- Introduction to eventsdeveloper.mozilla.org (opens in a new tab)
- Event bubblingdeveloper.mozilla.org (opens in a new tab)
- removeEventListener()developer.mozilla.org (opens in a new tab)
- stopPropagation()developer.mozilla.org (opens in a new tab)
- stopImmediatePropagation()developer.mozilla.org (opens in a new tab)
- focus eventdeveloper.mozilla.org (opens in a new tab)
- mouseenter eventdeveloper.mozilla.org (opens in a new tab)
- Event()developer.mozilla.org (opens in a new tab)
- CustomEvent()developer.mozilla.org (opens in a new tab)
- dispatchEvent()developer.mozilla.org (opens in a new tab)
- Why does a delegated listener on a list still handle items appended after it was registered?
- When does `stopPropagation()` still let another listener run on the same element?
- Why does a character counter wired to `change` on a `textarea` update only after the user clicks away?
- Which two ways make a listener run only on the first click?
- What does `event.detail` hold in a listener for `new CustomEvent('saved')` dispatched without options?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the JavaScript Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 24 lessons of the JavaScript Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (14 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams