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

XML DOM setAttributeNode() Method

XML DOM setAttributeNode() Method


Element Object Reference Element Object


Definition and Usage

The setAttributeNode() method adds a new attribute node.

If an attribute with the specified name already exists in the element, it will be replaced by the new attribute. If the new attribute replaces an existing attribute, the replaced attribute node is returned; otherwise, NULL is returned.

Syntax

elementNode.setAttributeNode(att_node)

Parameters Description
att_node Required. Specifies the attribute node to set.

Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc, and add the "edition" attribute to all elements:

Example

xmlDoc

=

loadXMLDoc

(

"

books.xml

"

)

;

newatt

=

xmlDoc

.

createAttribute

(

"

edition

"

)

;

newatt

.

nodeValue

=

"

first

"

;

x

=

xmlDoc

.

getElementsByTagName

(

"

title

"

)

;

x

[

0

]

.

setAttributeNode

(

newatt

)

;

document

.

write

(

"

Edition:

"

)

;

document

.

write

(

x

[

0

]

.

getAttribute

(

"

edition

"

)

)

;

Output:

Edition: first

Try it »


Element Object Reference Element Object