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/1-document/06-dom-attributes-and-properties/article.md
+19-18Lines changed: 19 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
When the browser loads the page, it "reads" (another word: "parses") HTML text and generates DOM objects from it. For element nodes most standard HTML attributes automatically become properties of DOM objects.
4
4
5
-
For instance, if the tag is `<body id="page">`, then the DOM object will have`body.id="page"`.
5
+
For instance, if the tag is `<body id="page">`, then the DOM object has`body.id="page"`.
6
6
7
-
But the mapping is not one-to-one! In this chapter we'll see that DOM properties and attributes are linked, but they may be different.
7
+
But the attribute-property mapping is not one-to-one! In this chapter we'll pay attention to separate these two notions, to see how to work with them, when they are same, and when they are different.
8
8
9
9
[cut]
10
10
@@ -55,7 +55,7 @@ So, DOM properties and methods behave just like those of regular JavaScript obje
55
55
56
56
In HTML language, tags may have attributes. When the browser reads HTML text and creates DOM objects for tags, it recognizes *standard* attributes and creates DOM properties from them.
57
57
58
-
So when an element has `id` or another standard attribute, the corresponding property gets created. But that doesn't happen if the attribute is non-standard.
58
+
So when an element has `id` or another *standard* attribute, the corresponding property gets created. But that doesn't happen if the attribute is non-standard.
59
59
60
60
For instance:
61
61
```html run
@@ -70,7 +70,7 @@ For instance:
70
70
</body>
71
71
```
72
72
73
-
Please note that a standard attribute for one element can be unknown for another one. For instance, `"type"` is standard for `<input>` ([HTMLInputElement](https://html.spec.whatwg.org/#htmlinputelement)), but not for `<body>` ([HTMLBodyElement](https://html.spec.whatwg.org/#htmlbodyelement)). Standard attributes are described in the specification for the corresponding class.
73
+
Please note that a standard attribute for one element can be unknown for another one. For instance, `"type"` is standard for `<input>` ([HTMLInputElement](https://html.spec.whatwg.org/#htmlinputelement)), but not for `<body>` ([HTMLBodyElement](https://html.spec.whatwg.org/#htmlbodyelement)). Standard attributes are described in the specification for the corresponding element class.
74
74
75
75
Here we can see it:
76
76
```html run
@@ -96,7 +96,7 @@ Sure. All attributes are accessible using following methods:
96
96
97
97
These methods operate exactly with what's written in HTML.
98
98
99
-
Also one can read all attributes using `elem.attributes`: a collection of objects that belong to a built-in [Attr](https://dom.spec.whatwg.org/#attr) class. It's enough to know that each of them has `name` and `value` properties.
99
+
Also one can read all attributes using `elem.attributes`: a collection of objects that belong to a built-in [Attr](https://dom.spec.whatwg.org/#attr) class, with`name` and `value` properties.
100
100
101
101
Here's a demo of reading a non-standard property:
102
102
@@ -164,7 +164,7 @@ In the example below `id` is modified as an attribute, and we can see the proper
164
164
</script>
165
165
```
166
166
167
-
But there are exclusions, for instance `input.value` synchronizes only from attribute to property:
167
+
But there are exclusions, for instance `input.value` synchronizes only from attribute -> to property, but not back:
168
168
169
169
```html run
170
170
<input>
@@ -188,11 +188,11 @@ In the example above:
188
188
- Changing the attribute `value` updates the property.
189
189
- But the property change does not affect the attribute.
190
190
191
-
That actually can come in handy, because the user may modify `value`, then, if we want to recover the "original" value from HTML, it's in the attribute.
191
+
That "feature" may actually can come in handy, because the user may modify `value`, and then after it, if we want to recover the "original" value from HTML, it's in the attribute.
192
192
193
193
## DOM properties are typed
194
194
195
-
DOM properties are not always strings. For instance, `input.checked` property (for checkboxes) and other similar properties are boolean:
195
+
DOM properties are not always strings. For instance, `input.checked` property (for checkboxes) is boolean:
196
196
197
197
```html run
198
198
<inputid="input"type="checkbox"checked> checkbox
@@ -218,9 +218,9 @@ There are other examples. The `style` attribute is a string, but `style` propert
218
218
</script>
219
219
```
220
220
221
-
But even if a DOM property type is a string, it may differ from the attribute.
221
+
That's an important difference. But even if a DOM property type is a string, it may differ from the attribute!
222
222
223
-
For instance, the `href` DOM property is always a full URL (by the standard), even if the attribute has a relative URL or just a `#hash`.
223
+
For instance, the `href` DOM property is always a *full* URL, even if the attribute contains a relative URL or just a `#hash`.
224
224
225
225
Here's an example:
226
226
@@ -235,14 +235,14 @@ Here's an example:
235
235
</script>
236
236
```
237
237
238
-
If we need the value of `href` or anything else exactly as written in the HTML, we need to use `getAttribute`.
238
+
If we need the value of `href` or any other attribute exactly as written in the HTML, we can use `getAttribute`.
239
239
240
240
241
241
## Non-standard attributes, dataset
242
242
243
243
When writing HTML, we use a lot of standard attributes. But what about non-standard, custom ones? First, let's see whether they are useful or not? What for?
244
244
245
-
Sometimes non-standard attributes are used to pass custom data from HTML to JavaScript, or "mark" elements.
245
+
Sometimes non-standard attributes are used to pass custom data from HTML to JavaScript, or to "mark" HTML-elements for JavaScript.
246
246
247
247
Like this:
248
248
@@ -300,15 +300,16 @@ For instance, here for the order state the attribute `order-state` is used:
300
300
</div>
301
301
```
302
302
303
-
Why the attribute was chosen in the example above, not classes like `.order-state-new`, `.order-state-pending`, `order-state-canceled`?
303
+
Why the attribute may be preferable to classes like `.order-state-new`, `.order-state-pending`, `order-state-canceled`?
304
304
305
305
That's because an attribute is more convenient to manage. The state can be changed as easy as:
306
306
307
307
```js
308
+
// a bit simpler than removing old/adding a new class
308
309
div.setAttribute('order-state', 'canceled');
309
310
```
310
311
311
-
But there may be a possible problem. What if we use a non-standard attribute for our purposes and later the standard introduces it and makes it do something? The HTML language is alive, it grows, more attributes appear to suit the needs of developers. There may be unexpected side-effects in case of such conflict.
312
+
But there may be a possible problem with custom attributes. What if we use a non-standard attribute for our purposes and later the standard introduces it and makes it do something? The HTML language is alive, it grows, more attributes appear to suit the needs of developers. There may be unexpected effects in such case.
312
313
313
314
To evade conflicts, there exist [data-*](https://html.spec.whatwg.org/#embedding-custom-non-visible-data-with-the-data-*-attributes) attributes.
314
315
@@ -353,13 +354,13 @@ Here's a rewritten "order state" example:
353
354
alert(order.dataset.orderState); // new
354
355
355
356
// modify
356
-
order.dataset.orderState="pending";
357
+
order.dataset.orderState="pending";// (*)
357
358
</script>
358
359
```
359
360
360
361
Using `data-*` attributes is a valid, safe way to pass custom data.
361
362
362
-
Please note that we can not only read, but also modify data-attributes. Then CSS updates the view accordingly: in the example above the last line changes the color to blue.
363
+
Please note that we can not only read, but also modify data-attributes. Then CSS updates the view accordingly: in the example above the last line `(*)`changes the color to blue.
363
364
364
365
## Summary
365
366
@@ -371,7 +372,7 @@ A small comparison:
371
372
|| Properties | Attributes |
372
373
|------------|------------|------------|
373
374
|Type|Any value, standard properties have types described in the spec|A string|
374
-
|Name|Name is case-sensitive|Name is case-insensitive|
375
+
|Name|Name is case-sensitive|Name is not case-sensitive|
375
376
376
377
Methods to work with attributes are:
377
378
@@ -381,7 +382,7 @@ Methods to work with attributes are:
381
382
-`elem.removeAttribute(name)` -- to remove the attribute.
382
383
-`elem.attributes` is a collection of all attributes.
383
384
384
-
We use attributes when DOM properties do not suit us and we need exactly attributes for some reasons, for instance:
385
+
For most needs DOM properties can serve us well. We should refer to attributes only when DOM properties do not suit us, when we need exactly attributes, for instance:
385
386
386
387
- We need a non-standard attribute. But if it starts with `data-`, then we should use `dataset`.
387
388
- We want to read the value "as written" in HTML. The value of the DOM property may be different, for instance `href` property is always a full URL, and we may want to get the "original" value.
0 commit comments