You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/2-events/01-introduction-browser-events/article.md
+39-40
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,13 @@ Here's a list of the most useful DOM events, just to take a look at:
11
11
-`mousedown` / `mouseup` -- when the mouse button is pressed / released over an element.
12
12
-`mousemove` -- when the mouse is moved.
13
13
14
+
**Keyboard events:**
15
+
-`keydown` and `keyup` -- when a keyboard key is pressed and released.
16
+
14
17
**Form element events:**
15
18
-`submit` -- when the visitor submits a `<form>`.
16
19
-`focus` -- when the visitor focuses on an element, e.g. on an `<input>`.
17
20
18
-
**Keyboard events:**
19
-
-`keydown` and `keyup` -- when the visitor presses and then releases the button.
20
-
21
21
**Document events:**
22
22
-`DOMContentLoaded` -- when the HTML is loaded and processed, DOM is fully built.
23
23
@@ -87,8 +87,6 @@ If the handler is assigned using an HTML-attribute then the browser reads it, cr
87
87
88
88
So this way is actually the same as the previous one.
89
89
90
-
**The handler is always in the DOM property: the HTML-attribute is just one of the ways to initialize it.**
91
-
92
90
These two code pieces work the same:
93
91
94
92
1. Only HTML:
@@ -109,6 +107,8 @@ These two code pieces work the same:
109
107
</script>
110
108
```
111
109
110
+
In the first example, the HTML attribute is used to initialize the `button.onclick`, while in the second example -- the script, that's all the difference.
111
+
112
112
**As there's only one `onclick` property, we can't assign more than one event handler.**
113
113
114
114
In the example below adding a handler with JavaScript overwrites the existing handler:
@@ -124,16 +124,6 @@ In the example below adding a handler with JavaScript overwrites the existing ha
124
124
</script>
125
125
```
126
126
127
-
By the way, we can assign an existing function as a handler directly:
128
-
129
-
```js
130
-
functionsayThanks() {
131
-
alert('Thanks!');
132
-
}
133
-
134
-
elem.onclick= sayThanks;
135
-
```
136
-
137
127
To remove a handler -- assign `elem.onclick = null`.
138
128
139
129
## Accessing the element: this
@@ -150,7 +140,17 @@ In the code below `button` shows its contents using `this.innerHTML`:
150
140
151
141
If you're starting to work with events -- please note some subtleties.
152
142
153
-
**The function should be assigned as `sayThanks`, not `sayThanks()`.**
143
+
We can set an existing function as a handler:
144
+
145
+
```js
146
+
functionsayThanks() {
147
+
alert('Thanks!');
148
+
}
149
+
150
+
elem.onclick= sayThanks;
151
+
```
152
+
153
+
But be careful: the function should be assigned as `sayThanks`, not `sayThanks()`.
154
154
155
155
```js
156
156
// right
@@ -160,29 +160,25 @@ button.onclick = sayThanks;
160
160
button.onclick=sayThanks();
161
161
```
162
162
163
-
If we add parentheses, `sayThanks()`-- is a function call. So the last line actually takes the *result* of the function execution, that is `undefined` (as the function returns nothing), and assigns it to `onclick`. That doesn't work.
163
+
If we add parentheses, then `sayThanks()`becomes is a function call. So the last line actually takes the *result* of the function execution, that is `undefined` (as the function returns nothing), and assigns it to `onclick`. That doesn't work.
164
164
165
165
...On the other hand, in the markup we do need the parentheses:
input.onclick=function() { alert(2); } // replaces the previous handler
212
208
```
213
209
214
-
Web-standard developers understood that long ago and suggested an alternative way of managing handlers using special methods `addEventListener` and `removeEventListener`. They are free of such a problem.
210
+
Developers of web standards understood that long ago and suggested an alternative way of managing handlers using special methods `addEventListener` and `removeEventListener`. They are free of such a problem.
- `once`: if `true`, then the listener is automatically removed after it triggers.
231
227
- `capture`: the phase where to handle the event, to be covered later in the chapter <info:bubbling-and-capturing>. For historical reasons, `options` can also be `false/true`, that's the same as `{capture: false/true}`.
232
-
- `passive`: if `true`, then the handler will not `preventDefault()`, we'll cover that later in <info:default-browser-action>.
233
-
228
+
- `passive`: if `true`, then the handler will not call `preventDefault()`, we'll explain that later in <info:default-browser-action>.
The handler won't be removed, because `removeEventListener` gets another function -- with the same code, but that doesn't matter.
247
+
The handler won't be removed, because `removeEventListener` gets another function -- with the same code, but that doesn't matter, as it's a different function object.
253
248
254
249
Here's the right way:
255
250
@@ -291,31 +286,33 @@ Multiple calls to `addEventListener` allow to add multiple handlers, like this:
291
286
As we can see in the example above, we can set handlers *both* using a DOM-property and `addEventListener`. But generally we use only one of these ways.
292
287
293
288
````warn header="For some events, handlers only work with `addEventListener`"
294
-
There exist events that can't be assigned via a DOM-property. Must use`addEventListener`.
289
+
There exist events that can't be assigned via a DOM-property. Only with`addEventListener`.
295
290
296
-
For instance, the event `DOMContentLoaded`, that triggers when the document is loaded and DOM is built.
291
+
For instance, the `DOMContentLoaded` event, that triggers when the document is loaded and DOM is built.
So `addEventListener` is more universal. Although, such events are an exception rather than the rule.
310
307
````
311
308
312
309
## Event object
313
310
314
-
To properly handle an event we'd want to know more about what's happened. Not just a "click" or a "keypress", but what were the pointer coordinates? Which key was pressed? And so on.
311
+
To properly handle an event we'd want to know more about what's happened. Not just a "click" or a "keydown", but what were the pointer coordinates? Which key was pressed? And so on.
315
312
316
313
When an event happens, the browser creates an *event object*, puts details into it and passes it as an argument to the handler.
317
314
318
-
Here's an example of getting mouse coordinates from the event object:
315
+
Here's an example of getting pointer coordinates from the event object:
319
316
320
317
```html run
321
318
<input type="button" value="Click me" id="elem">
@@ -338,11 +335,11 @@ Some properties of `event` object:
338
335
: Element that handled the event. That's exactly the same as `this`, unless the handler is an arrow function, or its `this` is bound to something else, then we can get the element from `event.currentTarget`.
339
336
340
337
`event.clientX / event.clientY`
341
-
: Window-relative coordinates of the cursor, for mouse events.
338
+
: Window-relative coordinates of the cursor, for pointer events.
342
339
343
-
There are more properties. They depend on the event type, so we'll study them later when we come to different events in details.
340
+
There are more properties. Many of them depend on the event type: keyboard events have one set of properties, pointer events - another one, we'll study them later when we come to different events in details.
344
341
345
-
````smart header="The event object is also accessible from HTML"
342
+
````smart header="The event object is also available in HTML handlers"
346
343
If we assign a handler in HTML, we can also use the `event` object, like this:
347
344
348
345
```html autorun height=60
@@ -364,15 +361,17 @@ For instance:
364
361
<buttonid="elem">Click me</button>
365
362
366
363
<script>
367
-
elem.addEventListener('click', {
364
+
let obj = {
368
365
handleEvent(event) {
369
366
alert(event.type+" at "+event.currentTarget);
370
367
}
371
-
});
368
+
};
369
+
370
+
elem.addEventListener('click', obj);
372
371
</script>
373
372
```
374
373
375
-
As we can see, when `addEventListener` receives an object as the handler, it calls `object.handleEvent(event)` in case of an event.
374
+
As we can see, when `addEventListener` receives an object as the handler, it calls `obj.handleEvent(event)` in case of an event.
0 commit comments