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
We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally.
Copy file name to clipboardExpand all lines: 2-ui/1-document/05-basic-dom-node-properties/article.md
+34-28
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
3
3
Let's get a more in-depth look at DOM nodes.
4
4
5
-
In this chapter we'll see more into what they are and their most used properties.
5
+
In this chapter we'll see more into what they are and learn their most used properties.
6
6
7
7
## DOM node classes
8
8
9
-
DOM nodes have different properties depending on their class. For instance, an element node corresponding to tag `<a>` has link-related properties, and the one corresponding to `<input>` has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
9
+
Different DOM nodes may have different properties. For instance, an element node corresponding to tag `<a>` has link-related properties, and the one corresponding to `<input>` has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
10
10
11
11
Each DOM node belongs to the corresponding built-in class.
12
12
@@ -21,22 +21,24 @@ The classes are:
21
21
-[EventTarget](https://dom.spec.whatwg.org/#eventtarget) -- is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
22
22
-[Node](http://dom.spec.whatwg.org/#interface-node) -- is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
23
23
-[Element](http://dom.spec.whatwg.org/#interface-element) -- is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. A browser supports not only HTML, but also XML and SVG. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
24
-
-[HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by various HTML elements:
24
+
-[HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by concrete HTML elements:
25
25
-[HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) -- the class for `<input>` elements,
26
26
-[HTMLBodyElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlbodyelement) -- the class for `<body>` elements,
27
-
-[HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements
27
+
-[HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements,
28
28
- ...and so on, each tag has its own class that may provide specific properties and methods.
29
29
30
30
So, the full set of properties and methods of a given node comes as the result of the inheritance.
31
31
32
-
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class. It gets properties and methods as a superposition of:
32
+
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class.
33
33
34
-
-`HTMLInputElement` -- this class provides input-specific properties, and inherits from...
35
-
-`HTMLElement` -- it provides common HTML element methods (and getters/setters) and inherits from...
36
-
-`Element` -- provides generic element methods and inherits from...
37
-
-`Node` -- provides common DOM node properties and inherits from...
34
+
It gets properties and methods as a superposition of (listed in inheritance order):
35
+
36
+
-`HTMLInputElement` -- this class provides input-specific properties,
37
+
-`HTMLElement` -- it provides common HTML element methods (and getters/setters),
38
+
-`Element` -- provides generic element methods,
39
+
-`Node` -- provides common DOM node properties,.
38
40
-`EventTarget` -- gives the support for events (to be covered),
39
-
- ...and finally it inherits from `Object`, so "pure object" methods like `hasOwnProperty` are also available.
41
+
- ...and finally it inherits from `Object`, so "plain object" methods like `hasOwnProperty` are also available.
40
42
41
43
To see the DOM node class name, we can recall that an object usually has the `constructor` property. It references the class constructor, and `constructor.name` is its name:
The `nodeType` property provides an old-fashioned way to get the "type" of a DOM node.
119
+
The `nodeType` property provides one more, "old-fashioned" way to get the "type" of a DOM node.
120
120
121
121
It has a numeric value:
122
122
- `elem.nodeType == 1` for element nodes,
@@ -185,8 +185,7 @@ For instance, let's compare `tagName` and `nodeName` for the `document` and a co
185
185
</body>
186
186
```
187
187
188
-
If we only deal with elements, then `tagName` is the only thing we should use.
189
-
188
+
If we only deal with elements, then we can use both `tagName` and `nodeName` - there's no difference.
190
189
191
190
```smart header="The tag name is always uppercase except XHTML"
192
191
The browser has two modes of processing documents: HTML and XML. Usually the HTML-mode is used for webpages. XML-mode is enabled when the browser receives an XML-document with the header: `Content-Type: application/xml+xhtml`.
In the line `(*)` we take the full HTML of `<div>...</div>` and replace it by `<p>...</p>`. In the outer document we can see the new content instead of the `<div>`. But the old `div` variable is still the same.
311
+
Looks really odd, right?
313
312
314
-
The `outerHTML` assignment does not modify the DOM element, but extracts it from the outer context and inserts a new piece of HTML instead of it.
313
+
In the line `(*)` we replaced `div` with `<p>A new element</p>`. In the outer document we can see the new content instead of the `<div>`. But, as we can see in line `(**)`, the old `div` variable is still the same!
315
314
316
-
Novice developers sometimes make an error here: they modify `div.outerHTML` and then continue to work with `div` as if it had the new content in it.
315
+
The `outerHTML` assignment does not modify the DOM element, but removes it from the outer context and inserts a new piece of HTML instead of it.
317
316
318
-
That's possible with `innerHTML`, but not with `outerHTML`.
317
+
So what happened in `div.outerHTML=...` is:
318
+
- `div` was removed from the document.
319
+
- Another HTML `<p>A new element</p>` was inserted instead.
320
+
- `div` still has the old value. The new HTML wasn't saved to any variable.
319
321
320
-
We can write to `outerHTML`, but should keep in mind that it doesn't change the element we're writing to. It creates the new content on its place instead. We can get a reference to new elements by querying DOM.
322
+
It's so easy to make an error here: modify `div.outerHTML` and then continue to work with `div` as if it had the new content in it. But it doesn't. Such thing is correct for `innerHTML`, but not for `outerHTML`.
323
+
324
+
We can write to `elem.outerHTML`, but should keep in mind that it doesn't change the element we're writing to. It creates the new HTML on its place instead. We can get references to new elements by querying DOM.
321
325
322
326
## nodeValue/data: text node content
323
327
324
328
The `innerHTML` property is only valid for element nodes.
325
329
326
-
Other node types have their counterpart: `nodeValue` and `data` properties. These two are almost the same for practical use, there are only minor specification differences. So we'll use `data`, because it's shorter.
330
+
Other node types, such as text nodes, have their counterpart: `nodeValue` and `data` properties. These two are almost the same for practical use, there are only minor specification differences. So we'll use `data`, because it's shorter.
327
331
328
332
An example of reading the content of a text node and a comment:
329
333
@@ -345,15 +349,17 @@ An example of reading the content of a text node and a comment:
345
349
</body>
346
350
```
347
351
348
-
For text nodes we can imagine a reason to read or modify them, but why comments? Usually, they are not interesting at all, but sometimes developers embed information or template instructions into HTML in them, like this:
352
+
For text nodes we can imagine a reason to read or modify them, but why comments?
353
+
354
+
Sometimes developers embed information or template instructions into HTML in them, like this:
349
355
350
356
```html
351
357
<!-- if isAdmin -->
352
358
<div>Welcome, Admin!</div>
353
359
<!-- /if -->
354
360
```
355
361
356
-
...Then JavaScript can read it and process embedded instructions.
362
+
...Then JavaScript can read it from `data` property and process embedded instructions.
357
363
358
364
## textContent: pure text
359
365
@@ -436,7 +442,7 @@ Here's a blinking element:
436
442
437
443
## More properties
438
444
439
-
DOM elements also have additional properties, many of them provided by the class:
445
+
DOM elements also have additional properties, in particular those that depend on the class:
440
446
441
447
- `value` -- the value for `<input>`, `<select>` and `<textarea>` (`HTMLInputElement`, `HTMLSelectElement`...).
442
448
- `href` -- the "href" for `<a href="...">` (`HTMLAnchorElement`).
@@ -457,7 +463,7 @@ For instance:
457
463
458
464
Most standard HTML attributes have the corresponding DOM property, and we can access it like that.
459
465
460
-
If we want to know the full list of supported properties for a given class, we can find them in the specification. For instance, HTMLInputElement is documented at <https://html.spec.whatwg.org/#htmlinputelement>.
466
+
If we want to know the full list of supported properties for a given class, we can find them in the specification. For instance, `HTMLInputElement` is documented at <https://html.spec.whatwg.org/#htmlinputelement>.
461
467
462
468
Or if we'd like to get them fast or are interested in a concrete browser specification -- we can always output the element using `console.dir(elem)` and read the properties. Or explore "DOM properties" in the Elements tab of the browser developer tools.
0 commit comments