Javascript DOM HTML Node innerText Property

Introduction

Get the inner text of an element:

var x = document.getElementById("myBtn").innerText;

Click the button get the text content of the button element.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()" id="myBtn">Test</button>
<p id="demo"></p>

<script>
function myFunction() {//from  www .j  a  va2s. c o  m
  var x = document.getElementById("myBtn").innerText;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The innerText property sets or gets the text content of the specified node, and all its descendants.

Setting the innerText property will replace child nodes by a single Text node containing the specified string.

To set or return the HTML content of an element, use the innerHTML property.




PreviousNext

Related