XML DOM textContent Property


❮ Node Object

Example

The following code fragment loads "books.xml" into xmlDoc and returns the textual content of the <book> elements:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
    var x, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

The output of the code above will be:

Everyday Italian Giada De Laurentiis 2005 30.00
Harry Potter J K. Rowling 2005 29.99
XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn
Vaidyanathan Nagarajan 2003 49.99
Learning XML Erik T. Ray 2003 39.95
Try it Yourself »

Definition and Usage

The textContent property sets or returns the textual content of a node and its descendants.

On setting, any child nodes are removed and replaced by a single Text node containing the string this property is set to.

Syntax

nodeObject.textContent

Try-It-Yourself Demos

textContent - Set the textual content of a node


❮ Node Object
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.