Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM createElement() Method

XML DOM createElement() Method


Document Object Reference Document Object


Definition and Usage

The createElement() method creates an element node.

This method returns an Element object.

Syntax

createElement(name)

Parameters Description
name A string that specifies the name of the element node.

Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc, and add the element node with a text node to each element:

Example

xmlDoc

=

loadXMLDoc

(

"

books.xml

"

)

;

x

=

xmlDoc

.

getElementsByTagName

(

"

book

"

)

;

for

(

i

=

0

;

i

<

x

.

length

;

i

++

)

{

newel

=

xmlDoc

.

createElement

(

"

edition

"

)

;

newtext

=

xmlDoc

.

createTextNode

(

"

first

"

)

;

newel

.

appendChild

(

newtext

)

;

x

[

i

]

.

appendChild

(

newel

)

;

}

//

Output all title and edition

y

=

xmlDoc

.

getElementsByTagName

(

"

title

"

)

;

z

=

xmlDoc

.

getElementsByTagName

(

"

edition

"

)

;

for

(

i

=

0

;

i

<

y

.

length

;

i

++

)

{

document

.

write

(

y

[

i

]

.

childNodes

[

0

]

.

nodeValue

)

;

document

.

write

(

"

"

)

;

document

.

write

(

z

[

i

]

.

childNodes

[

0

]

.

nodeValue

)

;

document

.

write

(

"


"

)

;

}

Try it »


Document Object Reference Document Object