XML DOM getAttribute() Method
Definition and usage
The getAttribute() method retrieves the value of the attribute by name.
Syntax:
elementNode.getAttribute(name)
Parameters | Description |
---|---|
name | Required. Specifies the attribute from which the attribute value is obtained. |
Tips and comments
For XML documents using namespaces, you need to use getAttributeNS() method.
Example
In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
The following code snippet retrieves the values of the "category" attribute of all <book> elements:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
for (i=0;i<x.length;i++)
{
document.write(x[i].getAttribute('category')
);
document.write("<br />");
}
The output of the above code is:
COOKING CHILDREN WEB WEB