The Node.textContent
property represents the text content of a node and its descendants. However, it is very different from the confusable innerText
property.
var text = element.textContent; element.textContent = "this is some sample text";
textContent
returns null
if 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 use document.documentElement.textContent
.textContent
returns the text inside this node (the nodeValue).textContent
returns the concatenation of the textContent
of every child node, excluding comments and processing instructions. This is an empty string if the node has no children.innerText
Internet Explorer introduced node.innerText
. The name is similar but with the following differences:
textContent
gets the content of all elements, including <script>
and <style>
elements, innerText
does not, only showing “human-readable” elements.innerText
is aware of styling and won’t return the text of “hidden” elements, whereas textContent
does.innerText
takes into account CSS styling, reading this property triggers a reflow, ensuring up-to-date computed style. textContent
doesn’t. Reflows can be computationally expensive thus should be avoided when possible.textContent
, altering innerText
in 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.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.
// 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>
// 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); } } ); })(); }
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes | Yes | 3 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | ? | Yes |
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 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent