0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Imagine we have two nested elements, each with a<em>click</em>event handler. If you click on an area of an external element that doesn't affect an internal one, then the handler bound to this external element will be executed.</p>
1
<p>Imagine we have two nested elements, each with a<em>click</em>event handler. If you click on an area of an external element that doesn't affect an internal one, then the handler bound to this external element will be executed.</p>
2
<p>If we click on the inner element, then the outer one will also be clicked on automatically, so both events will occur:</p>
2
<p>If we click on the inner element, then the outer one will also be clicked on automatically, so both events will occur:</p>
3
<p>You may wonder in what order these events will happen after clicking the button. In general, an event will pass through the tree from the root, go to the deepest element triggered by the event, and then in the opposite direction.</p>
3
<p>You may wonder in what order these events will happen after clicking the button. In general, an event will pass through the tree from the root, go to the deepest element triggered by the event, and then in the opposite direction.</p>
4
<p>The journey of an event up and down the tree is called its<strong>stages</strong>or<strong>phases</strong>. We will see more about that below.</p>
4
<p>The journey of an event up and down the tree is called its<strong>stages</strong>or<strong>phases</strong>. We will see more about that below.</p>
5
<h2>Capturing</h2>
5
<h2>Capturing</h2>
6
<p>When an event has just occurred, it starts moving through the DOM tree, starting from the root node and going to the deepest one where the event occurred:</p>
6
<p>When an event has just occurred, it starts moving through the DOM tree, starting from the root node and going to the deepest one where the event occurred:</p>
7
<p>| | ---------------| |--------------- | div | | | | -----------| |----------- | | | button \ / | | | ------------------------- | | Event CAPTURING | ---------------------------------</p>
7
<p>| | ---------------| |--------------- | div | | | | -----------| |----------- | | | button \ / | | | ------------------------- | | Event CAPTURING | ---------------------------------</p>
8
<p>At the capturing stage, we execute the handlers bound to this stage. The binding is regulated by the third parameter of the addEventListener function:</p>
8
<p>At the capturing stage, we execute the handlers bound to this stage. The binding is regulated by the third parameter of the addEventListener function:</p>
9
<p>The value true binds handlers to the capturing stage. The following output will result:</p>
9
<p>The value true binds handlers to the capturing stage. The following output will result:</p>
10
<p>Boom 2! Boom 1!</p>
10
<p>Boom 2! Boom 1!</p>
11
<h2>Bubbling</h2>
11
<h2>Bubbling</h2>
12
<p>Once the capturing stage ends at the<em>target</em>element, the bubbling stage begins:</p>
12
<p>Once the capturing stage ends at the<em>target</em>element, the bubbling stage begins:</p>
13
<p>/ \ ---------------| |--------------- | div | | | | -----------| |----------- | | | button | | | | | ------------------------- | | Event BUBBLING | ---------------------------------</p>
13
<p>/ \ ---------------| |--------------- | div | | | | -----------| |----------- | | | button | | | | | ------------------------- | | Event BUBBLING | ---------------------------------</p>
14
<p>We imply this stage when we call addEventListener without specifying the third parameter:</p>
14
<p>We imply this stage when we call addEventListener without specifying the third parameter:</p>
15
<p>At this stage, the handlers are executed from the inside out:</p>
15
<p>At this stage, the handlers are executed from the inside out:</p>
16
<p>Boom 1! Boom 2!</p>
16
<p>Boom 1! Boom 2!</p>
17
<h2>Why?</h2>
17
<h2>Why?</h2>
18
<p>Event bubbling is an important part of DOM behavior. Without it, it is impossible to implement events triggered on entire blocks, not just on the deepest elements.</p>
18
<p>Event bubbling is an important part of DOM behavior. Without it, it is impossible to implement events triggered on entire blocks, not just on the deepest elements.</p>
19
<p>The simplest example is a context menu. Another example could be tables arranged like in Microsoft Excel. These tables are huge, and adding events to each cell would create many identical handlers constantly added as the table grows. Besides the extra code, this strategy slows everything down if you work with many things. It's much easier to put one handler on the whole table.</p>
19
<p>The simplest example is a context menu. Another example could be tables arranged like in Microsoft Excel. These tables are huge, and adding events to each cell would create many identical handlers constantly added as the table grows. Besides the extra code, this strategy slows everything down if you work with many things. It's much easier to put one handler on the whole table.</p>
20
<h2>W3C Model</h2>
20
<h2>W3C Model</h2>
21
<p>According to the standard, most events go through both stages, first plunging into the depths of the tree and then rising to the very top. Programmers rarely use the capturing stage, so they hang most handlers on the bubbling one.</p>
21
<p>According to the standard, most events go through both stages, first plunging into the depths of the tree and then rising to the very top. Programmers rarely use the capturing stage, so they hang most handlers on the bubbling one.</p>
22
<p>In the previous lesson, we saw the e.target object. It is the deepest element that the capture stage goes to.</p>
22
<p>In the previous lesson, we saw the e.target object. It is the deepest element that the capture stage goes to.</p>
23
<p>The target element doesn't change during bubbling. It allows you to find out exactly where the event took place. In addition, the currentTarget object is also available; this is the element the handler is attached to. One or the other is used, depending on the situation:</p>
23
<p>The target element doesn't change during bubbling. It allows you to find out exactly where the event took place. In addition, the currentTarget object is also available; this is the element the handler is attached to. One or the other is used, depending on the situation:</p>
24
<p>Usually, the event should bubble to the end, but there are some situations where bubbling is undesirable.</p>
24
<p>Usually, the event should bubble to the end, but there are some situations where bubbling is undesirable.</p>
25
<p>There are two ways to do this:</p>
25
<p>There are two ways to do this:</p>
26
<ul><li>event.stopPropagation() - stops bubbling, but gives all the handlers that are hovering over the current element a chance to refine</li>
26
<ul><li>event.stopPropagation() - stops bubbling, but gives all the handlers that are hovering over the current element a chance to refine</li>
27
<li>event.stopImmediatePropagation() - doesn't allow us to execute any more handlers</li>
27
<li>event.stopImmediatePropagation() - doesn't allow us to execute any more handlers</li>
28
</ul><p>Let's summarize. Interception is a mechanism for passing an event to a handler. During capturing or bubbling, the event passes from one element to another, and if there is a handler for this event in the element, it gets called.</p>
28
</ul><p>Let's summarize. Interception is a mechanism for passing an event to a handler. During capturing or bubbling, the event passes from one element to another, and if there is a handler for this event in the element, it gets called.</p>
29
<p>In other words, the handler intercepts the event. And using the methods described above, we can stop the process of passing an event between elements.</p>
29
<p>In other words, the handler intercepts the event. And using the methods described above, we can stop the process of passing an event between elements.</p>