HomeTutorsContact
JavaScript
How To Get Node Value In Javascript?
Gulshan Saini
May 23, 2020
1 min

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

Next, we directly access the nodeValue property from the captured element as follows and log it on console

Output

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

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

Output

Here is, complete code


Tags

#dom-manipulation
Previous Article
How CSS Grid Layout Works?

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Related Posts

How to iterate through NodeList using Javascript?
July 09, 2020
1 min
© 2020, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media