Javascript DOM HTML Node nodeName Property along with node value and type

Introduction

Get the node name, node value and the node type of the <div> element's first child node:

Click the button get the node name, node type and node value of the div's first child.

View in separate window

<!DOCTYPE html>
<html>
<body>
<div id="myDIV">This is a div element.</div>
<br>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*w w  w  . j a  v a  2 s.  com*/
  var x = document.getElementById("myDIV").firstChild;
  var txt = "";
  txt += "The node name: " + x.nodeName + "<br>";
  txt += "The node value: " + x.nodeValue + "<br>";
  txt += "The node type: " + x.nodeType;
  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>



PreviousNext

Related