JavaScript Cheet Sheet

2.2 - HTML Objects

The following example demonstrates some Node Object Properties

This Heading's ID is heading1

This paragraph's ID is paragraph1

<h1 id="heading1" class="text-center">This Heading's ID is heading1</h1>

<p id="paragraph1" class="text-center">This paragraph's ID is paragraph1</p>

<script>
function showExample(){
   var h1Element = document.getElementById("heading1");

   var pElement;
   example3a.innerText = h1Element.nextSibling.nodeType;
   example3b.innerText = h1Element.nextSibling;
   example3c.innerText = h1Element.nextSibling.nextSibling;

   if (h1Element.nextSibling.nodeType == 1) {
   // An elements node type is 1, text nodes are 3
   // If the nextSibling's nodeType is 1, you assign that sibling's reference to pElement. If not, you get the next sibling (the <p> element) of h1Element's sibling (the whitespace text node).
   pElement = h1Element.nextSibling;
   } else {
   pElement = h1Element.nextSibling.nextSibling;
   // This is the code that runs and assigns pElement to paragraph1
   }
   example3d.innerText = pElement;
}
</script>

Before the codes if, else statement:

h1Element.nextSibling.nodeType =

h1Element.nextSibling =

h1Element.nextSibling.nextSibling =

After the if/else statement:

pElement =