Skip to content

Commit 4d65431

Browse files
committed
fix
1 parent 2b5c975 commit 4d65431

File tree

1 file changed

+34
-29
lines changed
  • 2-ui/1-document/04-searching-elements-dom

1 file changed

+34
-29
lines changed

2-ui/1-document/04-searching-elements-dom/article.md

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,71 @@ There are additional searching methods for that.
66

77
## document.getElementById or just id
88

9-
If an element has the `id` attribute, then there's a global variable by the name from that `id`.
9+
If an element has the `id` attribute, we can get the element using the method `document.getElementById(id)`, no matter where it is.
1010

11-
We can use it to immediately access the element no matter where it is:
11+
For instance:
12+
13+
```html run
14+
<div id="elem">
15+
<div id="elem-content">Element</div>
16+
</div>
17+
18+
<script>
19+
// get the element
20+
*!*
21+
let elem = document.getElementById('elem');
22+
*/!*
23+
24+
// make its background red
25+
elem.style.background = 'red';
26+
</script>
27+
```
28+
29+
Also, there's a global variable named by `id` that references the element:
1230

1331
```html run
1432
<div id="*!*elem*/!*">
1533
<div id="*!*elem-content*/!*">Element</div>
1634
</div>
1735

1836
<script>
19-
alert(elem); // DOM-element with id="elem"
20-
alert(window.elem); // accessing global variable like this also works
37+
// elem is a reference to DOM-element with id="elem"
38+
elem.style.background = 'red';
2139
22-
// for elem-content things are a bit more complex
23-
// that has a dash inside, so it can't be a variable name
24-
alert(window['elem-content']); // ...but accessible using square brackets [...]
40+
// id="elem-content" has a hyphen inside, so it can't be a variable name
41+
// ...but we can access it using square brackets: window['elem-content']
2542
</script>
2643
```
2744

28-
The behavior is described [in the specification](http://www.whatwg.org/specs/web-apps/current-work/#dom-window-nameditem), but it is supported mainly for compatibility. The browser tries to help us by mixing namespaces of JS and DOM. Good for very simple scripts, but there may be name conflicts. Also, when we look in JS and don't have HTML in view, it's not obvious where the variable comes from.
29-
30-
If we declare a variable with the same name, it takes precedence:
45+
...That's unless we declare a JavaScript variable with the same name, then it takes precedence:
3146

3247
```html run untrusted height=0
3348
<div id="elem"></div>
3449

3550
<script>
36-
let elem = 5;
51+
let elem = 5; // now elem is 5, not a reference to <div id="elem">
3752
3853
alert(elem); // 5
3954
</script>
4055
```
4156

42-
The better alternative is to use a special method `document.getElementById(id)`.
57+
```warn header="Please don't use id-named global variables to access elements"
58+
This behavior is described [in the specification](http://www.whatwg.org/specs/web-apps/current-work/#dom-window-nameditem), so it's kind of standard. But it is supported mainly for compatibility.
4359
44-
For instance:
60+
The browser tries to help us by mixing namespaces of JS and DOM. That's fine for simple scripts, inlined into HTML, but generally isn't a good thing. There may be naming conflicts. Also, when one reads JS code and doesn't have HTML in view, it's not obvious where the variable comes from.
4561
46-
```html run
47-
<div id="elem">
48-
<div id="elem-content">Element</div>
49-
</div>
62+
Here in the tutorial we use `id` to directly reference an element for brevity, when it's obvious where the element comes from.
5063
51-
<script>
52-
*!*
53-
let elem = document.getElementById('elem');
54-
*/!*
55-
56-
elem.style.background = 'red';
57-
</script>
64+
In real life `document.getElementById` is the preferred method.
5865
```
5966

60-
Here in the tutorial we'll often use `id` to directly reference an element, but that's only to keep things short. In real life `document.getElementById` is the preferred method.
61-
62-
```smart header="There can be only one"
67+
```smart header="The `id` must be unique"
6368
The `id` must be unique. There can be only one element in the document with the given `id`.
6469

65-
If there are multiple elements with the same `id`, then the behavior of corresponding methods is unpredictable. The browser may return any of them at random. So please stick to the rule and keep `id` unique.
70+
If there are multiple elements with the same `id`, then the behavior of methods that use it is unpredictable, e.g. `document.getElementById` may return any of such elements at random. So please stick to the rule and keep `id` unique.
6671
```
6772
68-
```warn header="Only `document.getElementById`, not `anyNode.getElementById`"
73+
```warn header="Only `document.getElementById`, not `anyElem.getElementById`"
6974
The method `getElementById` that can be called only on `document` object. It looks for the given `id` in the whole document.
7075
```
7176

0 commit comments

Comments
 (0)