XML DOM textContent property
Definition and Usage
The textContent property returns or sets the text of the selected element.
If text is returned, this property returns the value of all text nodes within the element node.
If text is set, this 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 comments:
Tip:If you need to return the text node text for IE browser, please use the text property.
Instance
In all the examples, we will use the XML file books.xml, as well as the JavaScript function loadXMLDoc().
Example 1
The following code snippet retrieves the text node of the first <title> element in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("title")[0];
document.write("Text Nodes: ");
document.write(x.textContent
);
The output of the above code is:
Text Nodes: Everyday Italian
Example 2
The following code snippet returns the text nodes from the first <book> element in "books.xml" and replaces all nodes with a new text node:
xmlDoc=loadXMLDoc("books.xml"); var 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 output of the above code is:
Before: Everyday Italian Giada De Laurentiis 2005 30.00 After: hello