Javascript DOM HTML Node textContent Property get

Introduction

Get all the textual content of an <ul> element with id="myList":

var x = document.getElementById("myList").textContent;

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

Textual content includes the text of all child nodes as well.

View in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList">
  <li>CSS</li>
  <li>HTML</li>
</ul>/*from   w ww  .  jav  a2s .  c om*/

<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myList").textContent;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related