0

I want to only hide the P0 paragraph using childNodes[x] . I wonder how it works because it hides the whole div with in this code:

<html>
<body>

<div id="myDiv">
 <p>P0</p>
 <p>P1</p>
</div>

<button onclick="hideFn();">hide</button>

<script>

function hideFn()
    {
      document.childNodes[0].childNodes[1].childNodes[1].style.display = "none";

    }

</script>

</body>
</html>
</html>
jess
  • 23
  • 7

1 Answers1

0

You easily could have found the reason yourself by simply doing the traversal step by step:

document

the document

.childNodes[0]

the documentElement, also known as the root node (<html>)

.childNodes[1]

the <body>

.childNodes[1]

the <div>

the8472
  • 40,999
  • 5
  • 70
  • 122
  • I tell you why I conuld not understand. I understood that the 1st child is which is childNodes[0]. And the 2nd child is and should be childNodes[0] becasue it is the first element inside . And the P0 should be childNodes[1] becase it is the 2nd child of the .......please correct me – jess Mar 14 '15 at 04:53
  • p0 is the child of div. try your browser's debugging tools, they'll show the DOM tree – the8472 Mar 14 '15 at 15:57