Javascript DOM How to - Get node name








Question

We would like to know how to get node name.

Answer


<!--  w ww . ja va2 s.  com-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){
    var all = document.body.getElementsByTagName("*");
    for (var i=0, max=all.length; i < max; i++) {
      console.log("all[i].nodeName:"+all[i].nodeName);
      console.log("all[i].nodeValue:"+all[i].nodeValue);
      for(var j = 0, max2 = all[i].childNodes.length; j < max2; j++) {
        console.log("all[i].childNodes[j].nodeValue:"+all[i].childNodes[j].nodeValue);
      }
    }
}
</script>
</head>
<body>
  <div>
    <span> $12.95 </span>
  </div>
</body>
</html>

The code above is rendered as follows: