getAttributes (DOMNode - JavaScript)

Gets the attributes of an element node.

Defined in

DOMNode

Syntax

getAttributes() : DOMNamedNodeMap

Return value Description
DOMNamedNodeMap A list of nodes representing the attributes.

Usage

If there are no attributes, the length of the list is 0. See getLength.

An attribute is returned as a node. UsegetNodeName and getNodeValue to get the name and value of the attribute.

Examples

This button gets the child nodes below the schema level of a DOM where requestScope.n is the location of a document in the database, and gets the attributes of each element.
if (requestScope.n != null
&& requestScope.n < database.getDocumentCount()
&& requestScope.n >= 0) {
	var dc = database.getAllDocuments();
	var doc = dc.getDocumentArray()[requestScope.n];
	var schema = doc.getFirstChild(); // get node below root
	requestScope.y = "Child nodes of " + schema.getNodeName();
	var element = schema.getFirstChild();
	while(element != null) {
		requestScope.y = requestScope.y +
			"\n\t" + element.getNodeName();
		if(element.hasAttributes()) {
			var map = element.getAttributes();
			for(var i=0; i< map.getLength(); i++) {
				var attr = map.item(i);
				requestScope.y = requestScope.y +
				"\n\t\t" + attr.getNodeName() + " = " + attr.getNodeValue();
			}
		} else {
			requestScope.y = requestScope.y +
				"\n\t\tHas no attributes";
		}
		element = element.getNextSibling();
	}
} else {
	requestScope.y = "No such document";
}
If the input XML to the DOM is as follows:
<schema0>
  <element0 City="Paris" Month="Mar">foo</element0>
  <element1 City="London">bar</element1>
  <element2>foobar</element2>
</schema0>
The display appears as follows:
Child nodes of schema0
	element0
		City = Paris
		Month = Mar
	element1
		City = London
	element2
		Has no attributes