Name

textContent: DOMString(3)

Synopsis

This attribute provides a quick method to get and set the textual content of nodes within the document tree. When used to retrieve text, it returns the contents of all descendant Text nodes without any whitespace normalization. Markup nodes (Attr, Element, and so forth) are ignored. When used to set text, it causes any child nodes to be removed and a single Text node to be created and linked in as the only child of the target node. The following table shows how the textContent attribute is constructed for the various node types:

Node type

Value

ELEMENT_NODE

ATTRIBUTE_NODE

ENTITY_NODE

ENTITY_REFERENCE_NODE

DOCUMENT_FRAGMENT_NODE

Combines the textContent of all child nodes, excluding nodes of type COMMENT_NODE and PROCESSING_INSTRUCTION_NODE, or an empty string if no child nodes

TEXT_NODE

CDATA_SECTION_NODE

COMMENT_NODE

PROCESSING_INSTRUCTION_NODE

Returns the same value as the nodeValue attribute

DOCUMENT_NODE

DOCUMENT_TYPE_NODE

NOTATION_NODE

Returns null

Exceptions

NO_MODIFICATION_ALLOWED_ERR

Thrown on set if node is read-only.

DOMSTRING_SIZE_ERR

Thrown on get when length of text content would exceed the maximum allowable length of a DOMString on the implementation platform.

Java binding

public String getTextContent( );
public void setTextContent(String textContent) throws DOMException

Methods

The following methods are defined for Node interface objects:

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.