X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM nodeValue Property

Element Object Reference Element Object

Example

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

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

The result of x will be:

Try it yourself

Try it yourself »

More "Try it Yourself" examples below.


Definition and Usage

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

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

Note: If you want to return the text of an element, remember that text is always inside a Text node, and you will have to return the Text node's node value (element.childNodes[0].nodeValue).

For other node types, the nodeValue property will return different values for different node types.

Tip: An alternative to the nodeValue property can be the textContent property.


Browser Support

Property
nodeValue Yes Yes Yes Yes Yes


Syntax

Return the node value:

node.nodeValue

Set the node value:

node.nodeValue=value

Property Values

Value Description
value  Specifies the node value of the specified node

Technical Details

Return Value: A String, representing the value of the node.

Possible values:

  • Returns null for element nodes and document nodes
  • Returns the value of the attribute for attribute nodes
  • Returns the content for text nodes
  • Returns the content for comment nodes
DOM Version Core Level 1 Node Object


Examples

More Examples

Example

Get the node name, node value and the node type of the <div> element's first child node:

<div id="myDIV">This is a div element.</div>

<script>
var x = document.getElementById("myDIV").firstChild;
var txt = "";
txt += "The node name: " + x.nodeName + "<br>";
txt += "The node value: " + x.nodeValue + "<br>";
txt += "The node type: " + x.nodeType;
</script>

The result of txt will be:

The node name: #text
The node value: This is a div element.
The node type: 3

Try it yourself »


Related Pages

HTML DOM reference: node.nodeName Property

HTML DOM reference: node.nodeType Property

HTML DOM reference: node.nodeValue Property

HTML DOM reference: node.childNodes Property


Element Object Reference Element Object


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook