3
3
## Notes
4
4
5
5
This exercises uses a set of nested ** divs** with a ** click** event attached
6
- to each of them.
6
+ to each of them:
7
+
7
8
``` html
8
9
<div class =" one" >
9
10
<div class =" two" >
@@ -21,7 +22,8 @@ divs.forEach(div => div.addEventListener('click', logText));
21
22
1 . The user clicks the ` <div class="three"> ` .
22
23
2 . Then the browser ripples down, so goes from the most external element to the deepest one and captures
23
24
all off the events binded to them. This process is called ** Event Capture** . This process has the aim to
24
- figure it out what the user has clicked on.
25
+ figure it out what the user has clicked on:
26
+
25
27
```javascript
26
28
// The browser stores the events in this order
27
29
// Event attached to <div class="one">
@@ -30,7 +32,8 @@ figure it out what the user has clicked on.
30
32
```
31
33
32
34
3 . At this moment the events are not fired yet. So starting from the bottom, the browser does something called ** bubble up**
33
- and fires each of these events.
35
+ and fires each of these events:
36
+
34
37
```javascript
35
38
// The browser fires the events in this order
36
39
// Event attached to <div class="three">
@@ -45,7 +48,8 @@ and fires each of these events.
45
48
}));
46
49
```
47
50
So now when the browser captures each of the events, it will inmediately fire them. That means that
48
- the handler for the event is not going to get run on the *buble up* but rather on the *capture down*.
51
+ the handler for the event is not going to get run on the *buble up* but rather on the *capture down*:
52
+
49
53
```javascript
50
54
// The browser fires the events in this order
51
55
// Event attached to <div class="one">
@@ -54,18 +58,20 @@ and fires each of these events.
54
58
```
55
59
56
60
4 . We can also call ** stop propagation** in the event handler, this way it will stop a ** buble up** process,
57
- firing only the deepest event, or viceversa.
61
+ firing only the deepest event, or viceversa:
62
+
58
63
```javascript
59
64
function logText(e) {
60
65
console.log(this.classList.value);
61
66
e.stopPropagation();
62
67
}
63
68
// now the the browser only fires the one event because capture=true
64
69
// Event attached to <div class="one">
65
- '''
70
+ ```
66
71
67
72
* Last but not least, ** Once** is a very new feature in the browser, that allows to listen for an event and then unbinds
68
73
itself, so the event will never be triggered again:
74
+
69
75
```javascript
70
76
divs.forEach(div => div.addEventListener('click', logText, {
71
77
capture: false,
0 commit comments