Node.textContent

  • Revision slug: DOM/Node.textContent
  • Revision title: Node.textContent
  • Revision id: 65709
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment 8 words added, 1 words removed

Revision Content

Summary

Gets or sets the text content of a node and its descendants.

Syntax

var text = element.textContent;
element.textContent = "this is some sample text";

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>

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.
  • Note that while textContent gets the content of all tags, including HTML <script> and <style> elements of HTML, the mostly equivalent IE-specific property, innerText, does not.
  • innerText is also aware of style and will not return the text of hidden elements, whereas textContent will

Browser Compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1+ {{ CompatVersionUnknown() }} 9 {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

Specification

textContent

{{ languages( { "fr": "fr/DOM/element.textContent", "it": "it/DOM/element.textContent", "pl": "pl/DOM/element.textContent" } ) }}

Revision Source

<h2 name="Summary">Summary</h2>
<p>Gets or sets the text content of a node and its descendants.</p>
<h2 name="Syntax">Syntax</h2>
<pre class="brush: js"><em>var text</em> = element.textContent;
element.textContent = "this is some sample text";
</pre>
<h2 name="Example">Example</h2>
<pre class="brush: js">// Given the following HTML fragment:
//   &lt;div id="divA"&gt;This is &lt;span&gt;some&lt;/span&gt; text&lt;/div&gt;

// 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:
//   &lt;div id="divA"&gt;This is some text&lt;/div&gt;
</pre>
<h2 name="Notes">Notes</h2>
<ul> <li><code>textContent</code> returns <code>null</code> 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<code> document.documentElement.textContent</code>.</li> <li>If the node is a CDATA section, a comment, a processing instruction, or a text node, <code>textContent</code> returns the text inside this node (the <a href="/En/DOM/Node.nodeValue" title="En/DOM/Node/NodeValue/Node.nodeValue">nodeValue</a>).</li> <li>For other node types, <code>textContent</code> returns the concatenation of the <code>textContent</code> attribute value of every child node, excluding comments and processing instruction nodes. This is an empty string if the node has no children.</li> <li>Setting this property on a node removes all of its children and replaces them with a single text node with the given value.</li> <li>Note that while <code>textContent</code> gets the content of all tags, including HTML <a href="/En/HTML/Element/Script">&lt;script&gt;</a> and <a href="/En/HTML/Element/Style">&lt;style&gt;</a> elements of HTML, the mostly equivalent IE-specific property, <code>innerText</code>, does not.</li> <li><code>innerText</code> is also aware of style and will not return the text of hidden elements, whereas textContent will</li>
</ul>
<h2>Browser Compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>1+</td> <td>{{ CompatVersionUnknown() }}</td> <td>9</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> </tr> </tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody>
</table>
</div><h2 name="Specification">Specification</h2>
<p><a class="external" href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent">textContent</a></p>
<p>{{ languages( { "fr": "fr/DOM/element.textContent", "it": "it/DOM/element.textContent", "pl": "pl/DOM/element.textContent" } ) }}</p>
Revert to this revision