XML DOM replaceData() Method

Definition and Usage

replaceData() Method to replace the data in the CDATA node.

Syntax

CDATANode.replaceData(start,length,string)
Parameter Description
start Required. Specifies the position at which to start replacing characters. The starting value is zero.
length Required. Specifies the number of characters to be replaced.
string Required. Specifies the string to be inserted.

Example

The following code loads "books_cdata.xml" into xmlDoc and replaces "Stunning" with "Fantastic" in the CDATA section of the first <html> 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];
    x.replaceData(3, 8, "Fantastic");
    document.getElementById("demo").innerHTML =
    x.data;
}

Try It Yourself