XML DOM textContent
Property
Definition and Usage
The textContent
property returns or sets the text of the selected element.
If returning text, this property returns the values of all text nodes within the element node.
If setting text, this property removes all child nodes and replaces them with a single text node.
Syntax
Return text:
Set text:
Tips and Notes
Tip: To set and return the text value of a node, use the nodeValue
property of the text node.
Example 1
The following code snippet uses loadXMLDoc()
to load "books.xml" into xmlDoc
and retrieves 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
Example 2
The following code snippet uses loadXMLDoc()
to load "books.xml" into xmlDoc
, retrieves the text node from the first <book>
element, and replaces 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