Skip to content

Commit 325e77e

Browse files
authored
Update readme.md
1 parent 30e719a commit 325e77e

File tree

1 file changed

+12
-6
lines changed
  • 25 - Event Capture, Propagation, Bubbling and Once

1 file changed

+12
-6
lines changed

25 - Event Capture, Propagation, Bubbling and Once/readme.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
## Notes
44

55
This exercises uses a set of nested **divs** with a **click** event attached
6-
to each of them.
6+
to each of them:
7+
78
```html
89
<div class="one">
910
<div class="two">
@@ -21,7 +22,8 @@ divs.forEach(div => div.addEventListener('click', logText));
2122
1. The user clicks the `<div class="three">`.
2223
2. Then the browser ripples down, so goes from the most external element to the deepest one and captures
2324
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+
2527
```javascript
2628
// The browser stores the events in this order
2729
// Event attached to <div class="one">
@@ -30,7 +32,8 @@ figure it out what the user has clicked on.
3032
```
3133

3234
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+
3437
```javascript
3538
// The browser fires the events in this order
3639
// Event attached to <div class="three">
@@ -45,7 +48,8 @@ and fires each of these events.
4548
}));
4649
```
4750
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+
4953
```javascript
5054
// The browser fires the events in this order
5155
// Event attached to <div class="one">
@@ -54,18 +58,20 @@ and fires each of these events.
5458
```
5559

5660
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+
5863
```javascript
5964
function logText(e) {
6065
console.log(this.classList.value);
6166
e.stopPropagation();
6267
}
6368
// now the the browser only fires the one event because capture=true
6469
// Event attached to <div class="one">
65-
'''
70+
```
6671

6772
* Last but not least, **Once** is a very new feature in the browser, that allows to listen for an event and then unbinds
6873
itself, so the event will never be triggered again:
74+
6975
```javascript
7076
divs.forEach(div => div.addEventListener('click', logText, {
7177
capture: false,

0 commit comments

Comments
 (0)