The Node.textContent property represents the text content of a node and its descendants. However, it is very different from the confusable innerText property.
Syntax
var text = element.textContent; element.textContent = "this is some sample text";
Description
textContentreturnsnullif the node is a document, a DOCTYPE, or a notation. To grab all of the text and CDATA data for the whole document, one could usedocument.documentElement.textContent.- If the node is a CDATA section, comment, processing instruction, or text node,
textContentreturns the text inside this node (the nodeValue). - For other node types,
textContentreturns the concatenation of thetextContentof every child node, excluding comments and processing instructions. 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.
Differences from innerText
Internet Explorer introduced node.innerText. The name is similar but with the following differences:
- While
textContentgets the content of all elements, including<script>and<style>elements,innerTextdoes not, only showing “human-readable” elements. innerTextis aware of styling and won’t return the text of “hidden” elements, whereastextContentdoes.- Since
innerTexttakes into account CSS styling, reading this property triggers a reflow, ensuring up-to-date computed style.textContentdoesn’t. Reflows can be computationally expensive thus should be avoided when possible. - Unlike
textContent, alteringinnerTextin Internet Explorer (version 11 and below) removes child nodes from the element and permanently destroys all descendant text nodes. It is impossible to insert the nodes again into any other element or into the same element anymore.
Differences from innerHTML
Element.innerHTML returns HTML as its name indicates. Sometimes people use innerHTML to retrieve or write text inside an element. textContent has better performance because its value is not parsed as HTML. Moreover, using textContent can prevent XSS attacks.
Example
// Given the following HTML fragment:
// <div id="divA">This is <span>some</span> text</div>
// Get the text content:
var text = document.getElementById("divA").textContent;
// |text| is set to "This is some text".
// Set the text content:
document.getElementById("divA").textContent = "This is some text";
// The HTML for divA is now:
// <div id="divA">This is some text</div>
Polyfill for IE8
// Source: Eli Grey @ https://eligrey.com/blog/post/textcontent-in-ie8
if (Object.defineProperty
&& Object.getOwnPropertyDescriptor
&& Object.getOwnPropertyDescriptor(Element.prototype, "textContent")
&& !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get) {
(function() {
var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
Object.defineProperty(Element.prototype, "textContent",
// Passing innerText or innerText.get directly does not work,
// wrapper function is required.
{
get: function() {
return innerText.get.call(this);
},
set: function(s) {
return innerText.set.call(this, s);
}
}
);
})();
}
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Basic support | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support 3 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS ? | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
Specifications
| Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'Node.textContent' in that specification. |
Living Standard | No change vs. DOM4 |
| DOM4 The definition of 'Node.textContent' in that specification. |
Obsolete | |
| Document Object Model (DOM) Level 3 Core Specification The definition of 'Node.textContent' in that specification. |
Obsolete | Introduced |
See also
Document Tags and Contributors
Tags:
Contributors to this page:
aleksander.wasaznik,
Tigt,
sideshowbarker,
IsaacAaron,
fscholz,
rileym7,
jdanyow,
PrivateFox,
Krinkle,
Lil_Devil,
nicknish,
abbycar,
Natalya_Surikova,
momdo,
chaoix,
Marat-Tanalin,
cvrebert,
krissy82476,
Sheppy,
kangax,
AlexAngas,
Agamemnus,
AlexChao,
teoli,
neilerdwien,
kscarfone,
Khodaidad_Basharmand,
ethertank,
yorkxin,
alex35,
dbruant,
paul.irish,
Brettz9,
zpao,
Matej Lednar,
Riquito,
Mgjbot,
Federico,
Nickolay,
RobG,
Fguisset
Last updated by:
aleksander.wasaznik,