XML DOM length attribute

Definition and Usage

length The property returns the number of characters in the selected node.

Syntax

CDATANode.length

Example

The following code loads "books_cdata.xml" into xmlDoc and retrieves the text node data and length from the first <title> element:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_cdata.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("html")[0].childNodes[0];
    document.getElementById("demo").innerHTML =
    x.data + " - Length: " + x.length;
}

Try It Yourself

Remember, the above length comes from the entire CDATA section (<b>Surprising!</b>), not just the text.