Event Buddling and Capturing
Last updated
Was this helpful?
Last updated
Was this helpful?
Capturing phase – the event goes down to the element.
Target phase – the event reached the target element.
Bubbling phase – the event bubbles up from the element.
When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.
The most deeply nested element that caused the event is called a target element, accessible as event.target
.
Note the differences from event.currentTarget:
event.target – is the “target” element that initiated the event, it doesn’t change through the bubbling process.
event.currentTarget – is the “current” element, the one that has a currently running handler on it.
In form.onclick handler:
event.currentTarget is the <form> element, because the handler runs on it.
event.target is the actual element inside the form that was clicked.
event.stopPropagation() stop event budding up to parent element
To catch an event on the capturing phase, we need to set the handler capture
option to true
:
Example