XML DOM createCDATASection() method

Document object reference manual

Definition and usage

The createCDATASection() method can create a CDATASection node.

This method can return a CDATASection object.

Syntax:

createCDATASection(data)
parameter description
data String value, this string can define data for this node.

Return value

Returns the newly created CDATASection node with the specified content.

throw

If the document is an HTML document, this method will throw a NOT_SUPPORTED_ERR code DOMException exception, because HTML documents do not support CDATASection nodes.

Example

In all examples, we will use the XML file books.xml, and JavaScript functions loadXMLDoc().

The following code snippet can add a CDATA section node to the <book> element:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newCDATA,newtext;
newtext="Special Offer & Book Sale";
for (i=0;i<x.length;i++)
  {
  newCDATA=xmlDoc.createCDATASection(newtext);;
  x[i].appendChild(newCDATA);
  }

Document object reference manual