Element nodeValue Property - Javascript DOM

Javascript examples for DOM:Element nodeValue

Description

The nodeValue property sets or gets the node value.

For element node, the nodeValue property will return null.

To return the text of an element, return the Text node's node value (element.childNodes[0].nodeValue).

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

Return the node value:

Set the node value with the following Values

Value Description
value? Sets the node value of the specified node

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

The following code shows how to get the node value of the first <button> element in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from w w  w .jav a2 s  .c o  m*/
    var c = document.getElementsByTagName("BUTTON")[0];
    var x = c.childNodes[0].nodeValue;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials