How To Get Node Value In Javascript?

By Gulshan Saini
Published in JavaScript
May 23, 2020
1 min read

The 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.

Getting node value

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 h1 element?

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[0]

The headingText variable will now store the value of h1 TEXT_NODE

Now, if we log the output on console we should get desired value



"What are node values?"

Here is, complete code


Previous Article
How CSS Grid Layout Works?

Related Posts

How to iterate through NodeList using Javascript?
July 09, 2020
1 min
Gulshan Saini

Gulshan Saini

Fullstack Developer



Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.
© 2021, All Rights Reserved.

Quick Links

Contact UsBrowserCSSPythonPuppeteer

Social Media