The Node.textContent
property represents the text content of a node and its descendants.
var text = element.textContent; element.textContent = "this is some sample text";
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
.textContent
returns the text inside this node (the nodeValue).textContent
returns the concatenation of the textContent
property value of every child node, excluding comments and processing instruction nodes. This is an empty string if the node has no children.innerText
Internet Explorer introduced element.innerText
. The intention is similar but with the following differences:
textContent
gets the content of all elements, including <script>
and <style>
elements, the IE-specific property innerText
does not.innerText
is aware of style and will not return the text of hidden elements, whereas textContent will.innerText
is aware of CSS styling, it will trigger a reflow, whereas textContent
will not.textContent
, altering innerText
in Internet Explorer (up to version 11 inclusive) not just removes child nodes from the element, but also permanently destroys all descendant text nodes (so it is impossible to insert the nodes again into any other element or into the same element anymore).innerHTML
innerHTML
returns the HTML as its name indicates. Quite often, in order to retrieve or write text within an element, people use innerHTML
. However, textContent
often has better performance because the text 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>
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", { get: function() { return innerText.get.call(this); }, set: function(s) { return innerText.set.call(this, s); } } ); })(); }
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1+ | (Yes) | 2 | 9 | 9.64 (possibly earlier) | 3 (possibly earlier) |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | (Yes) | ? | ? | ? | ? |
Specification | Status | Comment |
---|---|---|
DOM The definition of 'Node.textContent' in that specification. | Living Standard | No changes versus DOM4 |
DOM4 The definition of 'Node.textContent' in that specification. | Recommendation | |
Document Object Model (DOM) Level 3 Core Specification The definition of 'Node.textContent' in that specification. | Recommendation | Introduced |
© 2005–2017 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