Javascript DOM HTML Node nodeValue Property get

Introduction

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

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

Click the button get the node value of the first button element in the document.

Text inside elements are considered to be text nodes.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

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

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

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



PreviousNext

Related