HomeTutorsContact

How To Set Node Value In Javascript?

By Gulshan Saini
Published in JavaScript
May 24, 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.

Setting node value

Let’s start with the following markup

<h1 id="question">What are node values?</h1>

To set the node value of h1 tag first we need to select the elements TEXT_NODE as follows

const headingText = document.querySelector('h1').childNodes[0]

Next, we can use property nodeValue to set the contents if previously captured TEXT_NODE

headingText.nodeValue = "JavaScript is Awesome!!"

Output

Setting node value using javascript
Setting node value using javascript

Here is, complete code


Tags

#dom-manipulation
Previous Article
How To Get Node Value In Javascript?

Related Posts

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

Gulshan Saini

Fullstack Developer

Topics

Angular
JavaScript
ReactJS
Typescript

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