This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

textContent

Summary

Sets or retrieves the text content of a node and any child nodes.

Property of dom/Nodedom/Node

Syntax

var textContent = node.textContent;
node.textContent = newText;

Return Value

Returns an object of type StringString

The text content of a node and its child nodes, if any.

Examples

// Given the following HTML fragment:
//   <div id="divA">This is some text</div>

// Get the text content:
var text = document.getElementById("divA").textContent;
//

The following example displays the textContent property of a <script> tag.

<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script id="scrtest" type="text/javascript">
function gettextContent(el){
alert(el.textContent);
}
</script>
<title>textContent example</title>
</head>

<body>
<script type="text/javascript">
gettextContent(document.getElementById('scrtest'));
</script>



</body></html>

Notes

textContent returns null if the element is a document, a document type, or a notation. To grab all of the text and CDATA data for the whole document, one could use document.documentElement.textContent.

If the node is a CDATA section, a comment, a processing instruction, or a text node, textContent returns the text inside this node (the nodeValue).

For other node types, textContent returns the concatenation of the textContent attribute value of every child node, excluding comments and processing instruction nodes. This is an empty string if the node has no children.

Setting this property on a node removes all of its children and replaces them with a single text node with the given value.

Related specifications

DOM Level 3 Core
Recommendation

Attributions