Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM textContent Property

XML DOM textContent Property


Element Object Reference 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 get the text node of an element:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("title")[0];</p> <p>document.write("Text Nodes: ");</p> <p>document.write(x.textContent);</p> <p>The code above will output:</p> <p>Text Nodes: Everyday Italian</p> <p>Try it »</p> <hr /> <h2>Example 2</h2> <p>The following code snippet uses <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the " books.xml" loaded into xmlDoc, from the first element, get its text node, 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

(

"


"

)

;

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