Latest web development tutorials

XML DOM textContent property

Element Object Reference Element object

Definition and Usage

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

If the text is returned, the property returns the value of all text nodes within element nodes.

If you set the text, the attribute delete all child nodes and nodes with a single text to replace them.

grammar

Back text:

elementNode.textContent

Set the text:

elementNode.textContent=string


Tips and Notes

Tip: To set the value of the node and returns the text, use the text node nodeValue property .


Example 1

The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc and get text nodes from the first <title> element:

Examples

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

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

The code above will output:

Text Nodes: Everyday Italian

try it"

Example 2

The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc get text nodes from the first <book> element and replace all the nodes into a new text node:

Examples

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

document.write("Before: ");
document.write(x.textContent);

document.write("
");
x.textContent="hello";

document.write("After: ");
document.write(x.textContent);

The code above will output:

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

try it"

Element Object Reference Element object