Visit Mozilla.org

DOM:element.textContent

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

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

[edit] Syntax

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

[edit] 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>

[edit] Notes

  • textContent returns null if the element is a document, a document type, or a notation.
  • 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.

[edit] Specification

textContent