Element textContent Property - Javascript DOM

Javascript examples for DOM:Element textContent

Description

The textContent property sets or gets the textual content, and all its descendants.

Setting the textContent property will remove any child nodes and replace them with a single Text node containing the specified string.

Return the text content of a node:

Set the text content of a node with the following Values

Value Type Description
text String Sets the text content of the specified node

Return Value

A String, representing the text of the node and all its descendants

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList">
  <li id="item1">Coffee</li>
  <li id="item2">Tea</li>
</ul>/*  ww  w .j a v  a2  s.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>

Related Tutorials