XML DOM createElement() method
Definition and usage
The createElement() method can create an element node.
This method can return an Element object.
Syntax:
createElement(name)
parameters | description |
---|---|
name | String value, this string can specify the name of this element node. |
Return value
The newly created Element node has the specified tag name.
Throw
If the name parameter contains illegal characters, this method will throw an error code of INVALID_CHARACTER_ERR DOMException exception.
Instance
In all examples, we will use the XML file books.xml, as well as JavaScript functions loadXMLDoc().
The following code snippet can add an element node with a text node to each <book> element:
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel,newtext
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition')
;
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}