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
<p>In the line (*) we replaced div with <p>A new element</p>. In the outer document (the DOM) we can see the new content instead of the <div>. But, as we can see in line (**), the value of the old div variable hasn’t changed!</p>
345
348
346
-
In the line (*) we replaced div with <p>A new element</p>. In the outer document (the DOM) we can see the new content instead of the <div>. But, as we can see in line (**), the value of the old div variable hasn’t changed!
349
+
<p>The outerHTML assignment does not modify the DOM element (the object referenced by, in this case, the variable ‘div’), but removes it from the DOM and inserts the new HTML in its place.</p>
347
350
348
-
The outerHTML assignment does not modify the DOM element (the object referenced by, in this case, the variable ‘div’), but removes it from the DOM and inserts the new HTML in its place.
351
+
<p>So what happened in div.outerHTML=... is:</p>
349
352
350
-
So what happened in div.outerHTML=... is:
353
+
<p>div was removed from the document.</p>
351
354
352
-
div was removed from the document.
353
355
Another piece of HTML <p>A new element</p> was inserted in its place.
354
356
div still has its old value. The new HTML wasn’t saved to any variable.
355
357
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.
356
358
357
359
We can write to elem.outerHTML, but should keep in mind that it doesn’t change the element we’re writing to (‘elem’). It puts the new HTML in its place instead. We can get references to the new elements by querying the DOM.
358
360
359
-
nodeValue/data: text node content
361
+
<h3>nodeValue/data: text node content</h3>
360
362
The innerHTML property is only valid for element nodes.
361
363
362
364
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.
@@ -383,7 +385,9 @@ Sometimes developers embed information or template instructions into HTML in the
383
385
<!-- /if -->
384
386
…Then JavaScript can read it from data property and process embedded instructions.
The textContent provides access to the text inside the element: only text, minus all <tags>.
388
392
389
393
For instance:
@@ -422,11 +426,14 @@ The first <div> gets the name “as HTML”: all tags become tags, so we see the
422
426
The second <div> gets the name “as text”, so we literally see <b>Winnie-the-Pooh!</b>.
423
427
In most cases, we expect the text from a user, and want to treat it as text. We don’t want unexpected HTML in our site. An assignment to textContent does exactly that.
The “hidden” attribute and the DOM property specifies whether the element is visible or not.
427
433
428
434
We can use it in HTML or assign it using JavaScript, like this:
429
435
436
+
<pre>
430
437
<div>Both divs below are hidden</div>
431
438
432
439
<divhidden>With the attribute "hidden"</div>
@@ -436,16 +443,21 @@ We can use it in HTML or assign it using JavaScript, like this:
436
443
<script>
437
444
elem.hidden=true;
438
445
</script>
439
-
Technically, hidden works the same as style="display:none". But it’s shorter to write.
446
+
</pre>
447
+
448
+
<p>Technically, hidden works the same as style="display:none". But it’s shorter to write.</p>
440
449
441
-
Here’s a blinking element:
450
+
<p>Here’s a blinking element:</p>
442
451
452
+
<pre>
443
453
<divid="elem">A blinking element</div>
444
454
445
455
<script>
446
456
setInterval(() =>elem.hidden=!elem.hidden, 1000);
447
457
</script>
448
-
More properties
458
+
</pre>
459
+
460
+
<h3>More properties</h3>
449
461
DOM elements also have additional properties, in particular those that depend on the class:
450
462
451
463
value – the value for <input>, <select> and <textarea> (HTMLInputElement, HTMLSelectElement…).
@@ -454,115 +466,150 @@ id – the value of “id” attribute, for all elements (HTMLElement).
454
466
…and much more…
455
467
For instance:
456
468
469
+
<pre>
457
470
<inputtype="text"id="elem"value="value">
458
471
459
472
<script>
460
473
alert(elem.type); // "text"
461
474
alert(elem.id); // "elem"
462
475
alert(elem.value); // value
463
476
</script>
464
-
Most standard HTML attributes have the corresponding DOM property, and we can access it like that.
477
+
</pre>
478
+
479
+
<p>Most standard HTML attributes have the corresponding DOM property, and we can access it like that.</p>
480
+
481
+
<p>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.</p>
465
482
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.
483
+
<p>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.</p>
467
484
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.
<p>Each DOM node belongs to a certain class. The classes form a hierarchy. The full set of properties and methods come as the result of inheritance.</p>
469
489
470
-
Summary
471
-
Each DOM node belongs to a certain class. The classes form a hierarchy. The full set of properties and methods come as the result of inheritance.
490
+
<p>Main DOM node properties are:</p>
472
491
473
-
Main DOM node properties are:
474
492
475
493
nodeType
476
494
We can use it to see if a node is a text or an element node. It has a numeric value: 1 for elements,3 for text nodes, and a few others for other node types. Read-only.
495
+
477
496
nodeName/tagName
478
497
For elements, tag name (uppercased unless XML-mode). For non-element nodes nodeName describes what it is. Read-only.
498
+
479
499
innerHTML
480
500
The HTML content of the element. Can be modified.
501
+
481
502
outerHTML
482
503
The full HTML of the element. A write operation into elem.outerHTML does not touch elem itself. Instead it gets replaced with the new HTML in the outer context.
504
+
483
505
nodeValue/data
484
506
The content of a non-element node (text, comment). These two are almost the same, usually we use data. Can be modified.
507
+
485
508
textContent
486
509
The text inside the element: HTML minus all <tags>. Writing into it puts the text inside the element, with all special characters and tags treated exactly as text. Can safely insert user-generated text and protect from unwanted HTML insertions.
510
+
487
511
hidden
488
-
When set to true, does the same as CSS display:none.
489
-
DOM nodes also have other properties depending on their class. For instance, <input> elements (HTMLInputElement) support value, type, while <a> elements (HTMLAnchorElement) support href etc. Most standard HTML attributes have a corresponding DOM property.
512
+
<p>When set to true, does the same as CSS display:none.</p>
490
513
491
-
However, HTML attributes and DOM properties are not always the same, as we’ll see in the next chapter.
514
+
<p>DOM nodes also have other properties depending on their class. For instance, <input> elements (HTMLInputElement) support value, type, while <a> elements (HTMLAnchorElement) support href etc. Most standard HTML attributes have a corresponding DOM property.</p>
492
515
493
-
Tasks
494
-
Count descendants
495
-
importance: 5
496
-
There’s a tree structured as nested ul/li.
516
+
<p>However, HTML attributes and DOM properties are not always the same, as we’ll see in the next chapter.</p>
We also could examine the object using console.dir(document) and see these names by opening __proto__. The console takes them from constructor internally.
614
+
<p>We also could examine the object using <mark>console.dir(document)</mark> and see
615
+
these names by opening <mark>__proto__</mark>. The console takes them from <mark>constructor</mark> internally.</p>
0 commit comments