Javascript DOM How to - Output the childNode value








Question

We would like to know how to output the childNode value.

Answer


<!DOCTYPE html>
<html>
<body>
  <ul id="main">
    <li>
      <h2>Alec</h2>
      <p>NX-01</p>
      <p>command: 2151</p>
    </li><!--  w w w  . jav a  2  s .c  om-->
    <li>simple</li>
    <li>William</li>
  </ul>
<script type='text/javascript'>
var element = document.getElementById("main");
var values = element.childNodes[1].innerText; // the text simple this i want to output
console.log('the value is:' + values);
</script>

</body>
</html>

The code above is rendered as follows: