Event Buddling and Capturing
Overview

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.
Buddling
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.
<style>
body * {
margin: 10px;
border: 1px solid blue;
}
</style>
<form onclick="console.log('form')">FORM
<div onclick="console.log('div')">DIV
<p onclick="console.log('p')">P</p>
</div>
</form>
Output:
p div form
Targeting
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
Capturing
To catch an event on the capturing phase, we need to set the handler
captureoption totrue:
Example
Custom Event
Reference
Last updated
Was this helpful?