nodeValue

calendar November 30, 2009

Value: number, string, null

Compatibility: WinIE5+, MacIE5+, Moz1+, Safari1+

Of the node types implemented in the W3C DOM - capable browsers, only the text and attribute types have readable values. An element’s node value returns a null value. For an attribute node the nodeValue property consists of the value assigned to that attribute. As an example, nodeValue can be used to increase the width of a textarea object.

function increaseCols(elem)
{
  var colWidth = parseInt(elem.attributes["cols"].nodeValue, 10);
  elem.attributes["cols"].nodeValue = (colWidth * 1.1);
}

As another example you can replace the text of an element:

function replace(elem, newtxt)
{
  if(elem.childNodes.length == 1 && elem.firstChild.nodeType == 3)
  {
    elem.firstChild.nodeValue = newtxt;
  }
}

The function makes one final verification that the element contains one child node and that it is a text type.

Related items: attributes, innerText, nodeType properties.

admin

Leave a Reply

You must be logged in to post a comment.