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
+8-8
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ When the browser loads the page, it "reads" (another word: "parses") HTML text a
4
4
5
5
For instance, if the tag is `<body id="page">`, then the DOM object has `body.id="page"`.
6
6
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.
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 the same, and when they are different.
8
8
9
9
[cut]
10
10
@@ -28,11 +28,11 @@ alert(document.body.myData.title); // Imperator
28
28
We can add a method as well:
29
29
30
30
```js run
31
-
document.body.sayHi=function() {
31
+
document.body.sayTagName=function() {
32
32
alert(this.tagName);
33
33
};
34
34
35
-
document.body.sayHi(); // BODY (the value of "this" in the method is document.body)
35
+
document.body.sayTagName(); // BODY (the value of "this" in the method is document.body)
36
36
```
37
37
38
38
We can also modify built-in prototypes like `Element.prototype` and add new methods to all elements:
@@ -129,7 +129,7 @@ Here's an extended demo of working with attributes:
129
129
alert( elem.outerHTML ); // (3), see it's there
130
130
131
131
for (let attr ofelem.attributes) { // (4) list all
132
-
alert( attr.name+" = "+attr.value );
132
+
alert( `${attr.name} = ${attr.value}` );
133
133
}
134
134
</script>
135
135
</body>
@@ -144,9 +144,9 @@ Please note:
144
144
145
145
## Property-attribute synchronization
146
146
147
-
When a standard attribute changes, the corresponding property is auto-updated, and (with some exceptions) vise-versa.
147
+
When a standard attribute changes, the corresponding property is auto-updated, and (with some exceptions) vice versa.
148
148
149
-
In the example below `id` is modified as an attribute, and we can see the property change too. And then the same backwards:
149
+
In the example below `id` is modified as an attribute, and we can see the property changed too. And then the same backwards:
150
150
151
151
```html run
152
152
<input>
@@ -188,7 +188,7 @@ 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 "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.
191
+
That "feature" may actually 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.
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.
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
+
To avoid conflicts, there exist [data-*](https://html.spec.whatwg.org/#embedding-custom-non-visible-data-with-the-data-*-attributes) attributes.
315
315
316
316
**All attributes starting with "data-" are reserved for programmers' use. They are available in `dataset` property.**
0 commit comments