
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element nodeValue Property
The HTML DOM Element nodeValue property is used for accessing and updating the value of a node. When accessing nodeValue, ensure that the node has a child to access its value; otherwise you will get 'null'.
The HTML DOM Element nodeValue property is used to get (retrieve) and update (set) a node's value of an element in the document. In HTML, a node is a basic unit that makes up a document.
Syntax
Following is the syntax of the HTML DOM Element nodeValue (to set the property) −
node.nodeValue = newValue;
Here, the newValue is the value you want to set for nodeValue property.
Use the following syntax to get the nodeValue property −
node.nodeValue;
Parameters
Since this is a property, it does not accept any parameter.
Return Value
This property returns a string containing the text content of a node, If the node does not contain the text content it returns null.
Example 1: Retrieving Content Using nodeValue Property
The following program demonstrates the usage of the HTML DOM Element nodeValue property. It retrieves the <p> element content −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element nodeValue</title> </head> <body> <h3>HTML DOM Element nodeValue Property</h3> <p>Updates the text content within an element.</p> <div id="myDiv"> <p id="myg">This is the initial text content.</p> <button onclick="updateContent()">Get Content</button> </div> <script> function updateContent() { var para = document.getElementById('myg'); alert("Content: " + para.firstChild.nodeValue); } </script> </body> </html>
The above program displays the content of the paragraph (p) element.
Example 2: Setting the nodeValue Property
Here is another example of the HTML DOM Element nodeValue property. We use this property to set (update) the first list (<li>) item −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element nodeValue</title> </head> <body> <h3>HTML DOM Element nodeValue Example</h3> <p>Updates Text for First Node:</p> <ul id="myL"> <!-- Initial list items --> <li>This is item 1</li> <li>This is item 2</li> <li>This is item 3</li> </ul> <button onclick="updateItem()">Update First Item</button> <script> function updateItem() { var list = document.getElementById('myL'); // Use firstElementChild to skip text nodes var firstItem = list.firstElementChild; // Check if first child is an li element if (firstItem !== null) { firstItem.textContent = 'Updated text for item 1'; } } </script> </body> </html>
After running the program, a list of three items is displayed. When the button is clicked, the first item will be updated.
Example 3: Displays the Information About the First Child
This example shows how to access and display the information about the first child node of an element with the help of an nodeValue property within an HTML DOM Element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element nodeValue</title> </head> <body> <h3>HTML DOM Element nodeValue Property</h3> <p>Displays the information about the first Child Node:</p> <div id="my"> <!-- First child node --> <p>Content of the first child node.</p> </div> <div id="res"></div> <script> window.onload = function() { var divElement=document.getElementById('my'); var firstChild = divElement.firstChild; var nodeName = firstChild.nodeName; var nodeValue =firstChild.nodeValue||'N/A'; var nodeType = firstChild.nodeType; var resultDiv=document.getElementById('res'); resultDiv.innerHTML = '<p>Node Name: ' + nodeName + '</p>' + '<p>Node Value: ' + nodeValue + "Content of the first child node"+'</p>'+ '<p>Node Type: ' + nodeType + '</p>'; }; </script> </body> </html>
The above program displays the information about the first child node of an element.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
nodeValue | Yes | Yes | Yes | Yes | Yes |