This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

nodeValue

Summary

Gets or sets the value of a Node, if the type of Node supports it.

Property of dom/Nodedom/Node

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

Related specifications

DOM Level 3 Core
W3C Recommendation
WHATWG DOM
Living Standard

Attributions