XML DOM replaceData()Methods


Comment object

Definition and Usage

The replaceData() method replaces data in a comment node.

Syntax

commentNode.replaceData(start,length,string)

Parameter Description
start Required. Specifies where to replace characters. The start value begins at 0.
length Required. Specifies how many characters to replace.
string Required. Specifies the string to be inserted.


Example

The following code snippet usesloadXMLDoc()put "books_comment.xmlLoads into xmlDoc, and replaces "Simple" with "Easy" in the comment node of the first <book> element:

Example

xmlDoc=loadXMLDoc("books_comment.xml");

x=xmlDoc.getElementsByTagName("book")[0].childNodes;

for (i=0;i<x.length;i++)
{
if (x[i].nodeType==8)
{
//Process only comment nodes
x[i].replaceData(4,6,"Easy");
document.write(x[i].data);
document.write("
");
}
}

Output:

125 Easy and Delicious Recipes (Hardcover)

Try it Yourself »

In the example above, we use a loop and an if statement to perform processing only on comment nodes. The node type of comment nodes is 8.


Comment objectother extensions