PF 102 · Event-Driven Programming · Pre-final
Discussion
01 / 14
edp-lab/js/main.js
Who Ate the Exit Click?
Pre-final 03 — target, currentTarget, and bubbling on your own island
PF 102 · Pre-final Concept + Lab CLO3 · event flow Instructor: John Micheal Acera
Remember prelim?
Words you already know

Bubbling (default)

The event runs on the element you clicked first, then climbs up through its parents.

Capturing

Goes down from the top first. Only if you ask with { capture: true }.

preventDefault()

"Browser, skip your usual action" — e.g. don't submit the form.

stopPropagation()

"Don't tell my parents" — the event stops climbing.

Today you'll see bubbling break a real feature.
Who lives where
The page is a family tree
window
└── document
└── <html>
└── <body>
├── <div id="hud">
├── <div id="mode-bar">
│ └── <button id="exit-btn"> ← you click here
├── <aside id="event-trace">
└── <canvas> ← the 3D island (a sibling, not a parent!)
The Exit button sits on top of the canvas on screen — but in the tree it's a sibling.
The event object
Two questions every event answers
PropertyQuestion it answersExample
event.typeWhat happened?'click', 'dblclick', 'keydown'
event.targetWhat did the user actually click?the <button>
event.currentTargetWhose listener is running right now?the button — or window
event.codeWhich physical key?'Escape', 'KeyR'
When target and currentTarget are different, the event bubbled to get there.
Your X-ray
Reading the Event trace panel
click → onExitButtonClick target: exit-btn · listener on: exit-btn · mode: inside
  • click — event.type
  • onExitButtonClick — which of our handlers ran
  • target / listener on — event.target vs event.currentTarget
  • mode — our state at the moment the handler started
Break it on purpose
Move the click listener to window
// main.js — comment out the canvas listener… // edpRenderer.domElement.addEventListener('click', onClick); // …and listen on the whole window instead window.addEventListener('click', onClick);
  • 1Save → refresh
  • 2Double-click the Cafe to go inside
  • 3Click Exit building and watch the HUD + Event trace
What happened?
The "Back on the island" message vanished
click → onClick target: exit-btn · listener on: window · mode: island click → onExitButtonClick target: exit-btn · listener on: exit-btn · mode: inside
  • Newest line is on top: onExitButtonClick ran first, then onClick
  • onClick ran even though you never clicked the canvas
  • Its target is the button, but its listener lives on window
Why · bubbling
One click, climbing the family tree
① <button id="exit-btn"> TARGET → onExitButtonClick → mode = 'island'
↑ bubbles
<div id="mode-bar"> no listener
<body> → <html> → document no listeners
② window BUBBLE → onClick runs → misses every building → resetHud()
The canvas isn't in that path at all. It's a sibling, so a canvas listener would never hear this click.
Sneaky part
Why didn't the mode guard save us?
TIME 1
Button handler
exitInterior() sets mode = 'island' and shows "Back on the island".
TIME 2
Bubble reaches window
onClick checks mode === 'inside'? No — it's already 'island'.
TIME 3
Wrong job runs
It raycasts, hits nothing, calls resetHud() → your message is gone.
State was right. The listener was in the wrong place.
Three fixes
Pick one — and say why
FixCodeWhen it's right
A · listen on the right elementedpRenderer.domElement.addEventListener('click', onClick)Default. Only canvas clicks pick buildings
B · check the targetif (event.target !== edpRenderer.domElement) return;You need a page-wide listener for another reason
C · stop the bubbleevent.stopPropagation() in onExitButtonClickLast resort — see next slide
Put the canvas listener back (Fix A) before you leave.
Careful
Stop propagation only when justified
stopPropagation() hides the click from every parent — including listeners you add next month.
  • It treats the symptom (the bubble), not the cause (listener in the wrong place)
  • Fix the listener's location first (A). Check the target second (B). Stop last (C)
Don't mix them up
preventDefault vs stopPropagation
preventDefault()
Stops the browser's built-in action.
Form doesn't submit · Space doesn't scroll.
The event still bubbles.
stopPropagation()
Stops the event climbing to parents.
Parent listeners never hear it.
The browser's default action still happens.
Our Escape handler calls preventDefault(). On a normal page Escape does very little, but Space and arrow keys scroll — that's where it matters.
Paper drill · laptops closed
List every handler that runs, in order
  • 1I double-click the Cafe.
  • 2I'm inside and click Exit. (click listener is on the canvas)
  • 3Same, but the click listener is on window.
  • 4In #3, what are target and currentTarget inside onClick?
Next
Pre-final 04 — Inside checkpoint
  • →Canvas listener restored (Fix A) · Exit works cleanly
  • →Be ready to explain target vs currentTarget out loud
  • →Update README My Interiors · push your commits
The click wasn't eaten. It bubbled. Questions?
note //