XML DOM textContent Property
Element Object
Definition and Usage
The textContent property returns or sets the text of the selected element.
When returning text, this property returns the values of all text nodes within the element node.
When setting text, this property removes 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 a node's text value, use the text node's The nodeValue Property.
Example 1
The following code snippet uses loadXMLDoc() to load "books.xml" Load into xmlDoc, and from the first
Example
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 snippet uses loadXMLDoc() Put the "
Example
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