XML DOM textContentProperty


Element object

Definition and Usage

The textContent property returns or sets the text of the selected element.

If returning text, the property returns the value of all text nodes within the element node.

If setting text, the property deletes all child nodes and replaces them with a single text node.

Syntax

Return text:

elementNode.textContent

Set text:

elementNode.textContent=string


Tips and notes

Tip:To set and return the text value of a node, use the text node'snodeValue property。


Example 1

The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc and get the text node from the first <title> element:

Example

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];

document.write("Text Nodes: ");
document.write(x.textContent);

The above code will output:

Text Nodes: Everyday Italian

Try it Yourself »

Example 2

The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, get the text node from the first <book> element, and replace all nodes with a new text node:

Example

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; document.write("Before: "); document.write(x.textContent); document.write("<br>"); x.textContent="hello"; document.write("After: "); document.write(x.textContent);

The above code will output:

Before: Everyday Italian Giada De Laurentiis 2005 30.00
After: hello

Try it Yourself »

Element objectother extensions