@@ -102,7 +102,7 @@ alert( document.body instanceof HTMLElement ); // true
102102alert( document.body instanceof Element ); // true
103103alert( document.body instanceof Node ); // true
104104alert( document.body instanceof EventTarget ); // true
105- </ pre >
105+
106106
107107<p >As we can see, DOM nodes are regular JavaScript objects. They use prototype-based classes for inheritance.</p >
108108
@@ -374,156 +374,193 @@ here. Take a look.</p>
374374
375375<p >Looks really odd, right?</p >
376376
377- <p >In the line (*) we replaced div with <p >A new element</p >. In the outer document (the
378- DOM) we can see the new content instead of the <div >. But, as we can see in line (** ),
379- the value of the old div variable hasn’t changed!</p >
377+ <p >In the line <mark >(&ast ; )</mark > we replaced <mark >div</mark > with
378+ <mark >< ; p> ; A new element< ; /p> ; </mark >. In the outer document (the DOM) we can see
379+ the new content instead of the <mark >< ; div> ; </mark >. But, as we can see in line
380+ <mark >(&ast ;&ast ; )</mark >, the value of the old <mark >div</mark > variable hasn’t changed!</p >
380381
381- <p >The outerHTML assignment does not modify the DOM element (the object referenced by,
382+ <p >The < mark > outerHTML</ mark > assignment does not modify the DOM element (the object referenced by,
382383in this case, the variable ‘div’), but removes it from the DOM and inserts the new HTML
383384in its place.</p >
384385
385- <p >So what happened in div.outerHTML=... is:</p >
386-
387- <p >div was removed from the document.</p >
386+ <p >So what happened in <mark >div.outerHTML=...</mark > is:</p >
388387
389- Another piece of HTML <p >A new element</p > was inserted in its place.
390- div still has its old value. The new HTML wasn’t saved to any variable.
391- It’s so easy to make an error here: modify div.outerHTML and then continue to work
392- with div as if it had the new content in it. But it doesn’t. Such thing is correct
393- for innerHTML, but not for outerHTML.
388+ <ul >
389+ <li ><p ><mark >div</mark > was removed from the document.</p ></li >
390+ <li ><p >Another piece of HTML <mark >< ; p> ; A new element< ; /p> ; </mark > was inserted
391+ in its place.</li>
392+ <li ><mark >div</mark > still has its old value. The new HTML wasn’t saved to any variable.</li >
393+ </ul >
394394
395- We can write to elem.outerHTML, but should keep in mind that it doesn’t change the
396- element we’re writing to (‘elem’). It puts the new HTML in its place instead. We
397- can get references to the new elements by querying the DOM.
395+ < p >It’s so easy to make an error here: modify < mark >div.outerHTML</ mark > and then continue
396+ to work with < mark >div</ mark > as if it had the new content in it. But it doesn’t. Such
397+ thing is correct for < mark >innerHTML</ mark >, but not for < mark >outerHTML</ mark >.</ p >
398398
399+ <p >We can write to <mark >elem.outerHTML</mark >, but should keep in mind that it doesn’t
400+ change the element we’re writing to (‘elem’). It puts the new HTML in its place
401+ instead. We can get references to the new elements by querying the DOM.</p >
402+ <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
399403<h3 >nodeValue/data: text node content</h3 >
400- The innerHTML property is only valid for element nodes.
404+ <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
405+ <p >The <mark >innerHTML</mark > property is only valid for element nodes.</p >
401406
402- Other node types, such as text nodes, have their counterpart: nodeValue and data
403- properties. These two are almost the same for practical use, there are only minor
404- specification differences. So we’ll use data, because it’s shorter.
407+ < p > Other node types, such as text nodes, have their counterpart: < mark > nodeValue</ mark >
408+ and < mark >data</ mark > properties. These two are almost the same for practical use, there
409+ are only minor specification differences. So we’ll use < mark > data</ mark > , because it’s shorter.</ p >
405410
406- An example of reading the content of a text node and a comment:
411+ < p > An example of reading the content of a text node and a comment:</ p >
407412
408- <body >
413+ <pre >
414+ < ; body> ;
409415 Hello
410- < !-- Comment -->
411- < script >
416+ & lt ; !-- Comment --& gt ;
417+ & lt ; script& gt ;
412418 let text = document.body.firstChild;
413419 alert(text.data); // Hello
414420
415421 let comment = text.nextSibling;
416422 alert(comment.data); // Comment
417- </script >
418- </body >
419- For text nodes we can imagine a reason to read or modify them, but why comments?
423+ < ; /script> ;
424+ < ; /body> ;
425+ </pre >
426+
427+ <p >For text nodes we can imagine a reason to read or modify them, but why comments?</p >
420428
421- Sometimes developers embed information or template instructions into HTML in them, like
422- this:
429+ < p > Sometimes developers embed information or template instructions into HTML in them,
430+ like this:</ p >
423431
424- <!-- if isAdmin -->
425- <div >Welcome, Admin!</div >
426- <!-- /if -->
427- …Then JavaScript can read it from data property and process embedded instructions.
432+ <pre >
433+ < ; !-- if isAdmin --> ;
434+ < ; div> ; Welcome, Admin!< ; /div> ;
435+ < ; !-- /if --> ;
436+ </pre >
428437
438+ <p >…Then JavaScript can read it from <mark >data</mark > property and process embedded
439+ instructions.</p >
429440<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
430441<h3 >textContent: pure text</h3 >
431442<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
432- The textContent provides access to the text inside the element: only text, minus all <tags >.
443+ <p >The <mark >textContent</mark > provides access to the text inside the element: only
444+ text, minus all <mark >< ; tags> ; </mark >.
433445
434- For instance:
446+ < p > For instance:</ p >
435447
436- <div id =" news " >
437- <h1 >Headline!</h1 >
438- <p >Martians attack people!</p >
439- </div >
448+ <pre >
449+ < ; div id="news"> ;
450+ < ; h1> ; Headline!< ; /h1> ;
451+ < ; p> ; Martians attack people!< ; /p> ;
452+ < ; /div> ;
440453
441- < script >
454+ & lt ; script& gt ;
442455 // Headline! Martians attack people!
443456 alert(news.textContent);
444- </script >
445- As we can see, only text is returned, as if all <tags > were cut out, but the text in them
446- remained.
457+ < ; /script> ;
458+ </pre >
447459
448- In practice, reading such text is rarely needed.
460+ <p >As we can see, only text is returned, as if all <mark >< ; tags> ; </mark > were cut
461+ out, but the text in them remained.</p >
449462
450- Writing to textContent is much more useful, because it allows to write text the “safe way”.
463+ < p >In practice, reading such text is rarely needed.</ p >
451464
452- Let’s say we have an arbitrary string, for instance entered by a user, and want to show it.
465+ <h4 >Writing to <mark >textContent</mark > is much more useful, because it allows to write
466+ text the “safe way”.</h4 >
453467
454- With innerHTML we’ll have it inserted “as HTML”, with all HTML tags.
455- With textContent we’ll have it inserted “as text”, all symbols are treated literally.
456- Compare the two:
468+ <p >Let’s say we have an arbitrary string, for instance entered by a user, and want to show it.</p >
469+ <ul >
470+ <li >With <mark >innerHTML</mark > we’ll have it inserted “as HTML”, with all HTML tags.</li >
471+ <li >With <mark >textContent</mark > we’ll have it inserted “as text”, all symbols are
472+ treated literally.</li>
473+ </ul >
474+
475+ <p >Compare the two:</p >
457476
458- <div id =" elem1 " ></div >
459- <div id =" elem2 " ></div >
477+ <pre >
478+ < ; div id="elem1"> ;< ; /div> ;
479+ < ; div id="elem2"> ;< ; /div> ;
460480
461- < script >
462- let name = prompt (" What's your name?" , " <b> Winnie-the-Pooh!</b> " );
481+ & lt ; script& gt ;
482+ let name = prompt("What's your name?", "& lt ; b & gt ; Winnie-the-Pooh!& lt ; /b & gt ; ");
463483
464484 elem1.innerHTML = name;
465485 elem2.textContent = name;
466- </script >
467- The first <div > gets the name “as HTML”: all tags become tags, so we see the bold name.
468- The second <div > gets the name “as text”, so we literally see <b >Winnie-the-Pooh!</b >.
469- In most cases, we expect the text from a user, and want to treat it as text. We don’t
470- want unexpected HTML in our site. An assignment to textContent does exactly that.
486+ < ; /script> ;
487+ </pre >
471488
489+ <ol type =" 1 " >
490+ <li >The first <mark >< ; div> ; </mark > gets the name “as HTML”: all tags become tags,
491+ so we see the bold name.</li>
492+ <li >The second <mark >< ; div> ; </mark > gets the name “as text”, so we literally
493+ see <mark><b>Winnie-the-Pooh!</b></mark>.</li>
494+ </ol >
495+
496+ <p >In most cases, we expect the text from a user, and want to treat it as text. We don’t
497+ want unexpected HTML in our site. An assignment to <mark >textContent</mark > does exactly that.</p >
472498<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
473499<h3 >The “hidden” property</h3 >
474500<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
475- The “hidden” attribute and the DOM property specifies whether the element is visible or not.
501+ < p > The “hidden” attribute and the DOM property specifies whether the element is visible or not.</ p >
476502
477- We can use it in HTML or assign it using JavaScript, like this:
503+ < p > We can use it in HTML or assign it using JavaScript, like this:</ p >
478504
479505<pre >
480- < div > Both divs below are hidden< /div >
506+ & lt ; div& gt ; Both divs below are hidden& lt ; /div& gt ;
481507
482- < div hidden > With the attribute "hidden"< /div >
508+ & lt ; div hidden& gt ; With the attribute "hidden"& lt ; /div& gt ;
483509
484- < div id =" elem " > JavaScript assigned the property "hidden"< /div >
510+ & lt ; div id="elem"& gt ; JavaScript assigned the property "hidden"& lt ; /div& gt ;
485511
486- < script >
512+ & lt ; script& gt ;
487513 elem.hidden = true;
488- < /script >
514+ & lt ; /script& gt ;
489515</pre >
490516
491- <p >Technically, hidden works the same as style="display:none". But it’s shorter to write.</p >
517+ <p >Technically, <mark >hidden</mark > works the same as <mark >style="display:none"</mark >.
518+ But it’s shorter to write.</p >
492519
493520<p >Here’s a blinking element:</p >
494521
495522<pre >
496- < div id =" elem " > A blinking element< /div >
523+ & lt ; div id="elem"& gt ; A blinking element& lt ; /div& gt ;
497524
498- < script >
499- setInterval (() => elem .hidden = ! elem .hidden , 1000 );
500- < /script >
525+ & lt ; script& gt ;
526+ setInterval(() =& gt ; elem.hidden = !elem.hidden, 1000);
527+ & lt ; /script& gt ;
501528</pre >
502529
530+ <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
503531<h3 >More properties</h3 >
532+ <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
504533<p >DOM elements also have additional properties, in particular those that depend on the class:</p >
534+ <ul >
535+ <li ><mark >value</mark > – the value for <mark >< ; input> ; </mark >, <mark >< ; select> ; </mark >
536+ and <mark><textarea></mark> <mark>(HTMLInputElement, HTMLSelectElement</mark>…).</li>
537+ <li ><mark >href</mark > – the “href” for <mark >< ; a href="..."> ; (HTMLAnchorElement)</mark >.</li >
538+ <li ><mark >id</mark > – the value of “id” attribute, for all elements (<mark >HTMLElement</mark >).</li >
539+ <li >…and much more…</li >
540+ </ul >
505541
506- value – the value for <input >, <select > and <textarea > (HTMLInputElement, HTMLSelectElement…).
507- href – the “href” for <a href =" ... " > (HTMLAnchorElement).
508- id – the value of “id” attribute, for all elements (HTMLElement).
509- …and much more…
510- For instance:
542+ <p >For instance:</p >
511543
512544<pre >
513- < input type =" text " id =" elem " value =" value " >
545+ & lt ; input type="text" id="elem" value="value"& gt ;
514546
515- < script >
547+ & lt ; script& gt ;
516548 alert(elem.type); // "text"
517549 alert(elem.id); // "elem"
518550 alert(elem.value); // value
519- < /script >
551+ & lt ; /script& gt ;
520552</pre >
521553
522- <p >Most standard HTML attributes have the corresponding DOM property, and we can access it like that.</p >
554+ <p >Most standard HTML attributes have the corresponding DOM property, and we can access it
555+ like that.</p >
523556
524- <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 >
557+ <p >If we want to know the full list of supported properties for a given class, we can
558+ find them in the specification. For instance, <mark >HTMLInputElement</mark > is documented at
559+ <a href =" https://html.spec.whatwg.org/#htmlinputelement " >https://html.spec.whatwg.org/#htmlinputelement </a >.</p >
525560
526- <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 >
561+ <p >Or if we’d like to get them fast or are interested in a concrete browser specification
562+ – we can always output the element using <mark >console.dir(elem)</mark > and read the
563+ properties. Or explore “DOM properties” in the Elements tab of the browser developer tools.</p >
527564
528565<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
529566<h3 >Summary</h3 >
0 commit comments