The textContent
property sets or gets the textual content of the specified
node, and all its descendants.
If you set the textContent
property,
the child nodes are replaced by a single Text node containing the string.
textContent |
Yes | 9.0 | Yes | Yes | Yes |
Set the text content of a node.
node.textContent=text
Return the text content of a node.
var v = node.textContent
A String type value 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.
<!DOCTYPE html>
<html>
<body>
<p id="demo">text</p>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!-- ww w . ja va2 s .c o m-->
{
var c=document.getElementsByTagName("BUTTON")[0];
var x=document.getElementById("demo");
x.innerHTML=c.textContent;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get all the textual content of a list.
<!DOCTYPE html>
<html>
<body>
<!--from w w w .j a v a 2 s .c o m-->
<ul id="myList"><li id="item1">A</li><li id="item2">B</li></ul>
<p id="demo">test</p>
<button onclick="myFunction()">test</button>
<script>
function myFunction()
{
var lst=document.getElementById("myList");
var x=document.getElementById("demo");
x.innerHTML=lst.textContent;
}
</script>
</body>
</html>
The code above is rendered as follows: