XML DOM attributes eigenschap

Definitie en gebruik

attributes eigenschap retourneert een NamedNodeMap die de eigenschappen van de geselecteerde knoop bevat.

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 examples, we will use the XML file books.xml, and JavaScript functions 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