HTML DOM nodeValue Property

The nodeValue property sets or returns the node value of the specified node. The syntax of nodeValue property is as follows :
Also note that if the node is an element node, then the nodeValue property will return null because text inside elements are considered to be text nodes. So in order to get the nodeValue from HTML elements we have to use childNodes property. The childNodes property returns a collection of a node's child nodes, as a NodeList object. And by the below code we can get the nodeValue of an HTML element :
Example :
  <p id="demo" class="paragraph" style="color: blue;">Hello world</p>
  <button onclick="myfunc()">Click Here</button>
  <p id="echo1"></p>
  <p id="echo2"></p>
    function myfunc() {
      var val1 = document.getElementById("demo").childNodes[0].nodeValue;
      document.getElementById("echo1").innerText = "Paragraph value : " + val1;
      var val2 = document.getElementsByTagName("button")[0].childNodes[0].nodeValue;
      document.getElementById("echo2").innerText = "Button value : " + val2;
Output :

Hello world

Similarly theres other node properties like node.nodeName, node.nodeType, node.nodeValue etc.

Next Topic :