XML DOM attributes property

Definition and Usage

The 'attributes' property returns a NamedNodeMap containing the attributes of the selected node.

If the selected node is not an element, this property returns NULL.

Syntax:

elementNode.attributes

Tips and Comments

Tip:This attribute is only used for element nodes.

Instance

In all the examples, we will use the XML file books.xml, as well as the JavaScript function loadXMLDoc().

Example 1

The following code snippet gets the number of attributes of the first <title> element in "books.xml":

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book")[0].attributes;
document.write(x.length);

The output of the above code is:

1

Example 2

The following code snippet outputs the value of the "category" attribute of the first <book> element:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book")[0].attributes;
var att=x.getNamedItem("category");
document.write(att.value);

The output of the above code is:

COOKING