HTML DOM Element nodeValue property

Definition and usage

nodeValue The property sets or returns the value of the node.

If the node is an element node, then nodeValue The property will return null.

Note:If you want to return the text of an element, remember that the text is always located within a Text node and must return the node value of the Text node:

element.childNodes[0].nodeValue

For other node types:nodeValue The property will return different values for different node types.

Alternative:

textContent property

innerText property

innerHTML property

See also:

nodeName property

nodeType property

tagName property

childNodes property

Example

Example 1

Return the node value of the first <button> element in the document:

document.getElementsByTagName("BUTTON")[0].childNodes[0].nodeValue;

Try it yourself

Example 2

Get the node name, value, and type of the first child of "myDIV":

const x = document.getElementById("myDIV").firstChild;
let text = "";
text += "Name: " + x.nodeName + "<br>";
text += "Value: " + x.nodeValue + "<br>";
text += "Type: " + x.nodeType;

Try it yourself

Syntax

Return node value:

node.nodeValue

Set node value:

node.nodeValue = value

Attribute value

Value Description
value Node value.

Return value

Type Description
String

Node value.

  • Element and document nodes return null
  • Attribute node returns attribute value
  • Text node returns text content
  • Comment node returns text content

Browser support

element.nodeValue It is a DOM Level 1 (1998) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support