Element.getAttribute(name)
The getAttribute method returns a string containing the value of the specified
attribute. If the named attribute does not have a specified or default value, this method
returns null.
The following example uses the 'states.xml' file. Firstly a
NodeList is created of all the 'State' elements, and then
the code iterates through them getting the value of the 'ref' attribute of each.
XML:
<States>
<State ref="FL" const="27">
<name>Florida</name>
<capital>Tallahassee</capital>
</State>
<State ref="IA" const="29">
<name>Iowa</name>
<capital>Des Moines</capital>
</State>
</States>
Code (JavaScript):
xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("states.xml");
states = xml_doc.getElementsByTagName("State");
n_states = states.length;
for (i = 0; i < n_states; i++)
{
state = states.item(i);
attr = state.getAttribute("ref");
document.write(attr + "<br>");
}
Output:
FL IA
|