nodeValue
Summary
Gets or sets the value of a Node, if the type of Node supports it.
Syntax
var value = node.nodeValue;
node.nodeValue = newValue;
Return Value
Returns an object of type StringString
The value of the node.
Examples
The following code example alters the text of the specified list item by setting the nodeValue property of the text node that is contained by that list item.
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<script>
function changeValue(list, itemIndex, newValue) {
// only perform the operation on lists
if (list.nodeName !== "UL" && list.nodeName != "OL")
return false;
// only perform the operation if the specified index is
// within the acceptable range of available list items
if (itemIndex > list.childNodes.length -1)
return false;
// get a reference to the specified list item
var liElements = list.childNodes[itemIndex];
if (!liElements)
return false;
// get a reference to the text node contained by the list item
var textNode = liElements.childNodes[0];
// ensure that the node is a text node
if (textNode.nodeType !== 3)
return false;
textNode.nodeValue = newValue;
return true;
}
function initialize() {
document.getElementById("list").addEventListener(
"click",
function () {
changeValue(this, 0, "New Node value");
},
false);
}
document.addEventListener("DOMContentLoaded", initialize, false);
</script>
</head>
<body>
<ul id="list"><li>Old Node Value</li></ul>
</body>
Usage
Use this property to get or set the value of a Node. The concept of nodeValue changes between the various Node types (Element, Text and the rest).
Notes
- The value of this property for Text nodes is their Text.textContent.
- This property is deprecated for Attr nodes. Use Attr.value instead. The value of this property for Attr nodes is their Attr.value.
- The value of this property for Element nodes is always
null
. Use Node.nodeName to get their name or dom/Node/textContent to get all of the text included in their tree.
Related specifications
- DOM Level 3 Core
- W3C Recommendation
- WHATWG DOM
- Living Standard
Attributions
Mozilla Developer Network : [Node.nodeValue Article]
Microsoft Developer Network: [nodeValue Property Article]