HTML DOM Element nodeValue attribute

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 the element, remember that the text is always located within a Text node, and you 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 Solution:

textContent Property

innerText Property

innerHTML Property

See Also:

nodeName Property

nodeType Property

tagName Property

childNodes Property

Example

Example 1

Returns 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 the element "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
  • Returns attribute value of an attribute node
  • Returns text content of a text node
  • Returns text content of a comment node

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