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 | A string value, which 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 a code of INVALID_CHARACTER_ERR DOMException exception.
Example
In all examples, we will use the XML file books.xml, as well as JavaScript functions loadXMLDoc().
The following code snippet adds 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);
}