Bubbling and capturing
Let’s start with an example. This handler is assigned to
, but also runs if you click any nested tag like or
:
Isn’t it a bit strange? Why does the handler on run if the actual click was on ?
Bubbling
The bubbling principle is simple.
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.
Let’s say we have 3 nested elements FORM > DIV > P with a handler on each of them:
A click on the inner
first runs onclick:
1. On that
.
2. Then on the outer
.
3. Then on the outer