.eventPhase
Example
parent.addEventListener('click', (e) => {
if (e.eventPhase === Event.BUBBLING_PHASE) log('bubbling');
});eventPhase returns the current propagation phase, and Event.BUBBLING_PHASE ('3') matches a click bubbling up from a child.
Common mistakes
- Reading
eventPhaseafter the handler finished and getting 0.
Key points
- Code compares
eventPhaseto constants likeEvent.CAPTURING_PHASE,Event.AT_TARGETandEvent.BUBBLING_PHASE, not bare numbers. - Listeners that must run before target handlers are added with
{ capture: true }.
Interview questions
What are the possible values of eventPhase?
0 is NONE, 1 is CAPTURING_PHASE, 2 is AT_TARGET and 3 is BUBBLING_PHASE.
Why is eventPhase 0?
0 means NONE: the event is not being dispatched, for example when read after the handler returned or before dispatchEvent().
