The HTML DOM nodeValue Property is used to describe the property of a given node. It is used to set or get the nodeValue in Any Html document. Prerequisites DOM (Document Object Model)
Syntax
let x = document.getElementById("nodeId").firstChild;
x.nodeType;
x.nodeName;
x.nodeValue;
Parameters:Â No parameters are required.
Return value: The HTML nodeValue property returns the following type of valuesÂ
- null: for element and document nodes.
- attribute value: for any attribute.
- content: for any text node or comment node.
Example: Following example create a paragraph tag which is "divId"Â and provide a button that produces paragraph tag properties and returns its value to a tag whose id is "GeeksForGeeks".
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>HTML | DOM nodeValue Property</title>
</head>
<body style="text-align: center">
<h1 style="color: green;">GeeksforGeeks</h1>
<h2>DOM nodeValue Property</h2>
<p>
Click the button to get the node value of
the first button element in the document.
</p>
<!--Driver Code Ends-->
<button onclick="myFunction()">Try it</button>
<p id="geeks"></p>
<script>
function myFunction() {
let button = document.getElementsByTagName("button")[0];
let value = button.childNodes[0].nodeValue;
document.getElementById("geeks").textContent = value;
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- Modern code often prefers textContent (on elements) or the data property (on CharacterData subclasses like Text/Comment), but nodeValue is still the standardized, direct property on the base Node interface.
- Setting nodeValue on a text or comment node replaces its content, and it remains the property you encounter when working with older DOM traversal code or XML-style documents