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
There are also other rarely used methods of this kind:
125
125
126
126
- `document.getElementsByName(name)` returns elements with the given `name` attribute. Rarely used.
127
127
- `elem.getElementsByClassName(className)` returns elements that have the given CSS class. Elements may have other classes too.
@@ -179,7 +179,7 @@ Pseudo-classes in the CSS selector like `:hover` and `:active` are also supporte
179
179
180
180
The call to `elem.querySelector(css)` returns the first element for the given CSS selector.
181
181
182
-
In other words, the result is the same as `elem.querySelectorAll(css)[0]`, but that's looking for *all* elements and picking the first, much slower than only looking for the first one.
182
+
In other words, the result is the same as `elem.querySelectorAll(css)[0]`, but the latter is looking for *all* elements and picking one, while `elem.querySelector` just looks for one. So it's faster.
183
183
184
184
## matches
185
185
@@ -241,9 +241,11 @@ For instance:
241
241
242
242
## Live collections
243
243
244
-
All methods `getElementsBy*` return a *live* collection. They always reflect the current state of the document.
244
+
All methods `"getElementsBy*"` return a *live* collection. Such collections always reflect the current state of the document.
245
245
246
-
For instance, here in the first script the length is `1`, because the browser only processed the first div. Then later it's 2:
246
+
For instance, here in the first script the length is `1`, because the browser only processed the first div.
247
+
248
+
Then later after the second `div` it's 2:
247
249
248
250
```html run
249
251
<div>First div</div>
@@ -284,6 +286,10 @@ If we use it instead, then both scripts output `1`:
284
286
</script>
285
287
```
286
288
289
+
Now we can easily see the difference. The static collection did not increase after the appearance of a new `div` in the document.
290
+
291
+
Here we used separate scripts to illustrate how the element addition affects the collection. Soon we'll see more ways to alter DOM.
292
+
287
293
## Summary
288
294
289
295
There are 6 main methods to search for nodes in DOM:
@@ -292,8 +298,8 @@ There are 6 main methods to search for nodes in DOM:
292
298
<thead>
293
299
<tr>
294
300
<td>Method</td>
295
-
<td>Finds by...</td>
296
-
<td>Can call on element?</td>
301
+
<td>Searches by...</td>
302
+
<td>Can call on an element?</td>
297
303
<td>Live?</td>
298
304
</tr>
299
305
</thead>
@@ -337,9 +343,11 @@ There are 6 main methods to search for nodes in DOM:
337
343
</tbody>
338
344
</table>
339
345
340
-
Please note that methods `getElementById` and `getElementsByName` can only be called in the context of the document: `document.getElementById(...)`. Other methods can be called on elements, like `elem.querySelectorAll(...)` -- and will search in their subtrees.
346
+
Please note that methods `getElementById` and `getElementsByName` can only be called in the context of the document: `document.getElementById(...)`. But not on an element: `elem.getElementById(...)` would cause an error.
347
+
348
+
Other methods can be called on elements too. For instance `elem.querySelectorAll(...)` will search inside `elem` (in the DOM subtree).
341
349
342
-
Besides:
350
+
Besides that:
343
351
344
352
- There is `elem.matches(css)` to check if `elem` matches the given CSS selector.
345
353
- There is `elem.closest(css)` to look for a nearest ancestor that matches the given CSS-selector. The `elem` itself is also checked.
1. The content of `<body>` is replaced with the comment. The comment is <code><!--BODY--></code>, because `body.tagName == "BODY"`. As we remember, `tagName` is always uppercase in HTML.
16
+
2. The comment is now the only child node, so we get it in `body.firstChild`.
17
+
3. The `data` property of the comment is its contents (inside `<!--...-->`): `"BODY"`.
For built-in classes in all prototypes there's a `constructor` reference, and we can get `constructor.name` to see the name of the class. Let's do it for all objects in the `document` prototype chain:
We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally.
0 commit comments