nodeValue property sets or gets the node value of the specified node. The value returned by DOM API, depends on the type of the node.
Let’s start with the following markup
To capture the node value of
h1 tag first we need to select the element as follows and store it in some variable for reference
const heading = document.querySelector('h1')
Next, we directly access the
nodeValue property from the captured element as follows and log it on console
Now, you must be thinking why we have a value of
null when we have text inside the
We have an output of
null as the node is of type element and if the node is an element node, the nodeValue property will return null.
To capture inner text we need to first target the correct node as follows
const headingText = document.querySelector('h1').childNodes
headingText variable will now store the value of
Now, if we log the output on console we should get desired value
"What are node values?"
Here is, complete code