XML DOM replaceData() Method

Definition and Usage

replaceData() Method to replace data in a comment node.

Syntax

commentNode.replaceData(start,length,string)
Parameters 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 replace.
string Required. Specifies the string to be inserted.

Example

The following code loads "books_comment.xml" into xmlDoc and replaces "Simple" with "Easy" in the comment node of the first <book> element:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, txt, xmlDoc;
    xmlDoc = xml.responseXML;
   txt = "";
    x = xmlDoc.getElementsByTagName("book")[0].childNodes;
    for (i = 0; i < x.length; i++) {
    // Process only comment nodes
        if (x[i].nodeType == 8) {
            x[i].replaceData(4, 6, "Rahisi");
            txt += x[i].data + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

Jaribu Kupima

Kwenye mifano ya juu, tumetumia kikokoto na ifuri ya ifaa inayotestisha kuwa tu tumeongee mifano ya comment. Aina ya mifano ya comment ina aina ya node 8.