Skip to content

Commit 433b1ff

Browse files
authored
Update node-properties.md
1 parent 9dfbb9a commit 433b1ff

1 file changed

Lines changed: 50 additions & 19 deletions

File tree

node-properties.md

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -569,32 +569,40 @@ properties. Or explore “DOM properties” in the Elements tab of the browser d
569569

570570
<p>Main DOM node properties are:</p>
571571

572+
<mark>nodeType</mark>
573+
<p>We can use it to see if a node is a text or an element node. It has a numeric value:
574+
<mark>1</mark> for elements,<mark>3</mark> for text nodes, and a few others for other
575+
node types. Read-only.</p>
572576

573-
nodeType
574-
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.
577+
<mark>nodeName/tagName</mark>
578+
<p>For elements, tag name (uppercased unless XML-mode). For non-element nodes <mark>nodeName</mark>
579+
describes what it is. Read-only.</p>
575580

576-
nodeName/tagName
577-
For elements, tag name (uppercased unless XML-mode). For non-element nodes nodeName describes what it is. Read-only.
581+
<mark>innerHTML</mark>
582+
<p>The HTML content of the element. Can be modified.</p>
578583

579-
innerHTML
580-
The HTML content of the element. Can be modified.
584+
<mark>outerHTML</mark>
585+
<p>The full HTML of the element. A write operation into <mark>elem.outerHTML</mark> does not
586+
touch elem itself. Instead it gets replaced with the new HTML in the outer context.</p>
581587

582-
outerHTML
583-
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.
588+
<mark>nodeValue/data</mark>
589+
<p>The content of a non-element node (text, comment). These two are almost the same, usually we use data. Can be modified.</p>
584590

585-
nodeValue/data
586-
The content of a non-element node (text, comment). These two are almost the same, usually we use data. Can be modified.
591+
<mark>textContent</mark>
592+
<p>The text inside the element: HTML minus all <mark>&lt;tags&gt;</mark>. Writing into it
593+
puts the text inside the element, with all special characters and tags treated exactly as
594+
text. Can safely insert user-generated text and protect from unwanted HTML insertions.</p>
587595

588-
textContent
589-
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.
596+
<mark>hidden</mark>
597+
<p>When set to <mark>true</mark>, does the same as <mark>CSS display:none</mark>.</p>
590598

591-
hidden
592-
<p>When set to true, does the same as CSS display:none.</p>
593-
594-
<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>
599+
<p>DOM nodes also have other properties depending on their class. For instance,
600+
<mark>&lt;input&gt;</mark> elements <mark>(HTMLInputElement)</mark> support
601+
<mark>value</mark>, <mark>type</mark>, while <mark>&lt;a&gt;</mark> elements
602+
<mark>(HTMLAnchorElement)</mark> support <mark>href</mark> etc. Most standard
603+
HTML attributes have a corresponding DOM property.</p>
595604

596605
<p>However, HTML attributes and DOM properties are not always the same, as we’ll see in the next chapter.</p>
597-
598606
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
599607
<h2>Tasks</h2>
600608
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
@@ -609,13 +617,36 @@ hidden
609617
<li>The number of nested &lt;li&gt; – all descendants, including the deeply nested ones.</li>
610618
</ol>
611619

612-
<p>Demo in new window</p>
620+
<p><a href="https://en.js.cx/task/tree-info/solution/">Demo in new window</a>.</p>
613621

614-
<p>Open a sandbox for the task.</p>
622+
<p><a href="https://plnkr.co/edit/HILrCNC2WQL70wBY?p=preview">Open a sandbox for the task</a>.</p>
615623

616624
<h4>solution</h4>
625+
Let’s make a loop over <li>:
626+
<pre>
627+
for (let li of document.querySelectorAll('li')) {
628+
...
629+
}
630+
</pre>
631+
632+
<p>In the loop we need to get the text inside every <mark>li</mark>.</p>
633+
634+
<p>We can read the text from the first child node of <mark>li</mark>, that is the text node:</p>
617635

636+
<pre>
637+
for (let li of document.querySelectorAll('li')) {
638+
let title = li.firstChild.data;
639+
640+
// title is the text in <li> before any other nodes
641+
}
642+
</pre>
643+
644+
<p>Then we can get the number of descendants as <mark>li.getElementsByTagName('li').length</mark>.</p>
645+
646+
<p><a href="https://plnkr.co/edit/F1UQNdzRcv7xkTpk?p=preview">Open the solution in a sandbox</a>.</p>
647+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
618648
<h3>What's in the nodeType?</h3>
649+
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
619650
<small>importance: 5</small>
620651
<p>What does the script show?</p>
621652
<pre>

0 commit comments

Comments
 (0)