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/04-searching-elements-dom/article.md
+34-29Lines changed: 34 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -6,66 +6,71 @@ There are additional searching methods for that.
6
6
7
7
## document.getElementById or just id
8
8
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.
10
10
11
-
We can use it to immediately access the element no matter where it is:
11
+
For instance:
12
+
13
+
```html run
14
+
<divid="elem">
15
+
<divid="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:
12
30
13
31
```html run
14
32
<divid="*!*elem*/!*">
15
33
<divid="*!*elem-content*/!*">Element</div>
16
34
</div>
17
35
18
36
<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';
21
39
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']
25
42
</script>
26
43
```
27
44
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:
31
46
32
47
```html run untrusted height=0
33
48
<divid="elem"></div>
34
49
35
50
<script>
36
-
let elem =5;
51
+
let elem =5;// now elem is 5, not a reference to <div id="elem">
37
52
38
53
alert(elem); // 5
39
54
</script>
40
55
```
41
56
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.
43
59
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.
45
61
46
-
```html run
47
-
<divid="elem">
48
-
<divid="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.
50
63
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.
58
65
```
59
66
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"
63
68
The `id` must be unique. There can be only one element in the document with the given `id`.
64
69
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.
66
71
```
67
72
68
-
```warn header="Only `document.getElementById`, not `anyNode.getElementById`"
73
+
```warn header="Only `document.getElementById`, not `anyElem.getElementById`"
69
74
The method `getElementById` that can be called only on `document` object. It looks for the given `id` in the whole document.
0 commit comments