Javascript DOM How to - Check the difference between children and childNodes








Question

We would like to know how to check the difference between children and childNodes.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.4.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from   w w  w. jav a2s .  c  o m-->
    console.log(document.getElementById('dd').children.length);
    console.log(document.getElementById('dd').childNodes.length);
});
</script>
</head>
<body>
  <div id="dd">
    <p>Test paragraph.</p>
    <div>
      <p>Test paragraph 2.</p>
    </div>
    Text.
  </div>
</body>
</html>

The code above is rendered as follows: