XML DOM setAttributeNS()Methods


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 in the element, its value is changed to the value of the prefix and the value parameter.

Syntax

elementNode.setAttributeNS(ns,name,value)

Parameter Description
ns Required. Specifies the namespace URI of the attribute to be set.
name Required. Specifies the name of the attribute to be set.
value Required. Specifies the value of the property to be set.


Example 1

The following code snippet usesloadXMLDoc()put "books_ns.xmlLoad into xmlDoc, and add an "edition" attribute to the first <book> element:

Example

xmlDoc=loadXMLDoc("books_ns.xml"); x=xmlDoc.getElementsByTagName("book")[0]; ns="http://www.example.com/w3cnote/"; x.setAttributeNS(ns,"edition","first"); document.write(x.getAttributeNS(ns,"edition"));

Output:

first

Try it Yourself »

Example 2

The following code snippet usesloadXMLDoc()put "books_ns.xmlLoad into xmlDoc, and change the "lang" value of the first <title> element:

Example

xmlDoc=loadXMLDoc("books_ns.xml"); x=xmlDoc.getElementsByTagName("title")[0]; ns="http://www.example.com/w3cnote/"; x.setAttributeNS(ns,"c:lang","italian"); document.write(x.getAttributeNS(ns,"lang"));

Output:

italian

Try it Yourself »

Element objectother extensions