Skip to content

Commit b32a124

Browse files
authored
Update node-properties.md
1 parent 508e73d commit b32a124

1 file changed

Lines changed: 117 additions & 70 deletions

File tree

node-properties.md

Lines changed: 117 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ removed. And so on.</p>
311311

312312
<p>Luckily, there are other ways to add HTML besides innerHTML, and we’ll study them soon.</p>
313313

314+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
314315
<h3><a href="https://javascript.info/basic-dom-node-properties#outerhtml-full-html-of-the-element">
315316
outerHTML: full HTML of the element</a></h3>
316-
317+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
317318
<p>The outerHTML property contains the full HTML of the element. That’s like innerHTML
318319
plus the element itself.</p>
319320

@@ -324,11 +325,11 @@ plus the element itself.</p>
324325
<script>
325326
alert(elem.outerHTML); // <div id="elem">Hello <b>World</b></div>
326327
</script>
327-
Beware: unlike innerHTML, writing to outerHTML does not change the element. Instead, it replaces it in the DOM.
328+
<p>Beware: unlike innerHTML, writing to outerHTML does not change the element. Instead, it replaces it in the DOM.</p>
328329

329-
Yeah, sounds strange, and strange it is, that’s why we make a separate note about it here. Take a look.
330+
<p>Yeah, sounds strange, and strange it is, that’s why we make a separate note about it here. Take a look.</p>
330331

331-
Consider the example:
332+
<p>Consider the example:</p>
332333

333334
<div>Hello, world!</div>
334335

@@ -341,22 +342,23 @@ Consider the example:
341342
// Wow! 'div' is still the same!
342343
alert(div.outerHTML); // <div>Hello, world!</div> (**)
343344
</script>
344-
Looks really odd, right?
345+
<p>Looks really odd, right?</p>
346+
347+
<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>
345348

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>
347350

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>
349352

350-
So what happened in div.outerHTML=... is:
353+
<p>div was removed from the document.</p>
351354

352-
div was removed from the document.
353355
Another piece of HTML <p>A new element</p> was inserted in its place.
354356
div still has its old value. The new HTML wasn’t saved to any variable.
355357
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.
356358

357359
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.
358360

359-
nodeValue/data: text node content
361+
<h3>nodeValue/data: text node content</h3>
360362
The innerHTML property is only valid for element nodes.
361363

362364
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
383385
<!-- /if -->
384386
…Then JavaScript can read it from data property and process embedded instructions.
385387

386-
textContent: pure text
388+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
389+
<h3>textContent: pure text</h3>
390+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
387391
The textContent provides access to the text inside the element: only text, minus all <tags>.
388392

389393
For instance:
@@ -422,11 +426,14 @@ The first <div> gets the name “as HTML”: all tags become tags, so we see the
422426
The second <div> gets the name “as text”, so we literally see <b>Winnie-the-Pooh!</b>.
423427
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.
424428

425-
The “hidden” property
429+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
430+
<h3>The “hidden” property</h3>
431+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
426432
The “hidden” attribute and the DOM property specifies whether the element is visible or not.
427433

428434
We can use it in HTML or assign it using JavaScript, like this:
429435

436+
<pre>
430437
<div>Both divs below are hidden</div>
431438

432439
<div hidden>With the attribute "hidden"</div>
@@ -436,16 +443,21 @@ We can use it in HTML or assign it using JavaScript, like this:
436443
<script>
437444
elem.hidden = true;
438445
</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>
440449

441-
Here’s a blinking element:
450+
<p>Here’s a blinking element:</p>
442451

452+
<pre>
443453
<div id="elem">A blinking element</div>
444454

445455
<script>
446456
setInterval(() => elem.hidden = !elem.hidden, 1000);
447457
</script>
448-
More properties
458+
</pre>
459+
460+
<h3>More properties</h3>
449461
DOM elements also have additional properties, in particular those that depend on the class:
450462

451463
value – the value for <input>, <select> and <textarea> (HTMLInputElement, HTMLSelectElement…).
@@ -454,115 +466,150 @@ id – the value of “id” attribute, for all elements (HTMLElement).
454466
…and much more…
455467
For instance:
456468

469+
<pre>
457470
<input type="text" id="elem" value="value">
458471

459472
<script>
460473
alert(elem.type); // "text"
461474
alert(elem.id); // "elem"
462475
alert(elem.value); // value
463476
</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>
465482

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>
467484

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.
485+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
486+
<h3>Summary</h3>
487+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
488+
<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>
469489

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>
472491

473-
Main DOM node properties are:
474492

475493
nodeType
476494
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+
477496
nodeName/tagName
478497
For elements, tag name (uppercased unless XML-mode). For non-element nodes nodeName describes what it is. Read-only.
498+
479499
innerHTML
480500
The HTML content of the element. Can be modified.
501+
481502
outerHTML
482503
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+
483505
nodeValue/data
484506
The content of a non-element node (text, comment). These two are almost the same, usually we use data. Can be modified.
507+
485508
textContent
486509
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+
487511
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>
490513

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, &lt;input&gt; elements (HTMLInputElement) support value, type, while <a> elements (HTMLAnchorElement) support href etc. Most standard HTML attributes have a corresponding DOM property.</p>
492515

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>
497517

498-
Write the code that for each <li> shows:
518+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
519+
<h2>Tasks</h2>
520+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
521+
<h3>Count descendants</h3>
522+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
523+
<small>importance: 5</small>
524+
<p>There’s a tree structured as nested ul/li.</p>
499525

500-
What’s the text inside it (without the subtree)
501-
The number of nested <li> – all descendants, including the deeply nested ones.
502-
Demo in new window
526+
<p>Write the code that for each &lt;li&gt; shows:</p>
527+
<ol type="1">
528+
<li>What’s the text inside it (without the subtree)</li>
529+
<li>The number of nested &lt;li&gt; – all descendants, including the deeply nested ones.</li>
530+
</ol>
503531

504-
Open a sandbox for the task.
532+
<p>Demo in new window</p>
505533

506-
solution
507-
What's in the nodeType?
508-
importance: 5
509-
What does the script show?
534+
<p>Open a sandbox for the task.</p>
510535

511-
<html>
536+
<h4>solution</h4>
512537

513-
<body>
514-
<script>
538+
<h3>What's in the nodeType?</h3>
539+
<small>importance: 5</small>
540+
<p>What does the script show?</p>
541+
<pre>
542+
&lt;html&gt;
543+
544+
&lt;body&gt;
545+
&lt;script&gt;
515546
alert(document.body.lastChild.nodeType);
516-
</script>
517-
</body>
547+
&lt;/script&gt;
548+
&lt;/body&gt;
518549

519-
</html>
520-
solution
521-
Tag in comment
522-
importance: 3
523-
What does this code show?
550+
&lt;/html&gt;
551+
</pre>
524552

525-
<script>
553+
<h4>solution</h4>
554+
555+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
556+
<h3>Tag in comment</h3>
557+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
558+
<small>importance: 3</small>
559+
560+
<p>What does this code show?</p>
561+
562+
<pre>
563+
&lt;script&gt;
526564
let body = document.body;
527565

528-
body.innerHTML = "<!--" + body.tagName + "-->";
566+
body.innerHTML = "&lt;!--" + body.tagName + "--&gt;";
529567

530568
alert( body.firstChild.data ); // what's here?
531-
</script>
532-
solution
533-
Where's the "document" in the hierarchy?
569+
&lt;/script&gt;
570+
</pre>
571+
572+
<h4>solution</h4>
573+
<h3>Where's the "document" in the hierarchy?</h3>
534574
importance: 4
535-
Which class does the document belong to?
536575

537-
What’s its place in the DOM hierarchy?
576+
<h4>Which class does the document belong to?</h4>
577+
578+
<p>What’s its place in the DOM hierarchy?</p>
538579

539-
Does it inherit from Node or Element, or maybe HTMLElement?
580+
<p>Does it inherit from Node or Element, or maybe HTMLElement?</p>
540581

541-
solution
542-
We can see which class it belongs by outputting it, like:
582+
<h4>solution</h4>
543583

544-
alert(document); // [object HTMLDocument]
545-
Or:
584+
<p>We can see which class it belongs by outputting it, like:</p>
546585

547-
alert(document.constructor.name); // HTMLDocument
548-
So, document is an instance of HTMLDocument class.
586+
<pre>alert(document); // [object HTMLDocument]</pre>
587+
<p>Or:</p>
549588

550-
What’s its place in the hierarchy?
589+
<pre>alert(document.constructor.name); // HTMLDocument</pre>
590+
<p>So, document is an instance of HTMLDocument class.</p>
551591

552-
Yeah, we could browse the specification, but it would be faster to figure out manually.
592+
<p>What’s its place in the hierarchy?</p>
553593

554-
Let’s traverse the prototype chain via __proto__.
594+
<p>Yeah, we could browse the specification, but it would be faster to figure out manually.</p>
555595

556-
As we know, methods of a class are in the prototype of the constructor. For instance, HTMLDocument.prototype has methods for documents.
596+
<p>Let’s traverse the prototype chain via __proto__.</p>
557597

558-
Also, there’s a reference to the constructor function inside the prototype:
598+
<p>As we know, methods of a class are in the prototype of the constructor. For instance, HTMLDocument.prototype has methods for documents.</p>
559599

560-
alert(HTMLDocument.prototype.constructor === HTMLDocument); // true
561-
To get a name of the class as a string, we can use constructor.name. Let’s do it for the whole document prototype chain, till class Node:
600+
<p>Also, there’s a reference to the constructor function inside the prototype:</p>
562601

602+
<pre>alert(HTMLDocument.prototype.constructor === HTMLDocument); // true</pre>
603+
604+
<p>To get a name of the class as a string, we can use constructor.name. Let’s do it for the whole document prototype chain, till class Node:</p>
605+
606+
<pre>
563607
alert(HTMLDocument.prototype.constructor.name); // HTMLDocument
564608
alert(HTMLDocument.prototype.__proto__.constructor.name); // Document
565609
alert(HTMLDocument.prototype.__proto__.__proto__.constructor.name); // Node
566-
That’s the hierarchy.
610+
</pre>
611+
612+
<p>That’s the hierarchy.</p>
567613

568-
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

Comments
 (0)