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

XML DOM setAttributeNS() Method

XML DOM setAttributeNS() Method


Element Object Reference Element Object


Definition and Usage

The setAttributeNS() method adds a new attribute (with a namespace).

If an attribute with the specified name or namespace already exists on the element, its value is changed to the values of the prefix and value parameters.

Syntax

elementNode.setAttributeNS(ns,name,value)

Parameters Description
ns Required. Specifies the namespace URI of the attribute to set.
name Required. Specifies the name of the attribute to set.
value Required. Specifies the value of the attribute to set.

Example 1

The following code snippet uses loadXMLDoc() to load "books_ns.xml" Load into xmlDoc, and to the first elements:

Example

xmlDoc

=

loadXMLDoc

(

"

books_ns.xml

"

)

;

x

=

xmlDoc

.

getElementsByTagName

(

"

book

"

)

[

0

]

;

ns

=

"

http://www.runoob.com/w3cnote/

"

;

x

.

setAttributeNS

(

ns

,

"

edition

"

,

"

first

"

)

;

document

.

write

(

x

.

getAttributeNS

(

ns

,

"

edition

"

)

)

;

Output:

first

Try it »


Example 2

The following code snippet uses loadXMLDoc() to load "books_ns.xml" Load into xmlDoc, and change the "lang" value of the first element: </p>

Example

xmlDoc

=

loadXMLDoc

(

"

books_ns.xml

"

)

;

x

=

xmlDoc

.

getElementsByTagName

(

"

title

"

)

[

0

]

;

ns

=

"

http://www.runoob.com/w3cnote/

"

;

x

.

setAttributeNS

(

ns

,

"

c:lang

"

,

"

italian

"

)

;

document

.

write

(

x

.

getAttributeNS

(

ns

,

"

lang

"

)

)

;

Output:

italian

Try it »


Element Object Reference Element Object