FREE Web Template Download
HTML CSS JAVASCRIPT SQL PHP BOOTSTRAP JQUERY ANGULARJS TUTORIALS REFERENCES EXAMPLES Blog
 

XML DOM getAttributeNode() Method


Element Object Reference Element Object

Example

The following code fragment loads "books.xml" into xmlDoc and gets the "category" attribute from all <book> elements:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (xhttp.readyState == 4 && xhttp.status == 200) {
       myFunction(xhttp);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
    var x, i, attnode, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for (i = 0; i < x.length; i++) {
        attnode = x.item(i).getAttributeNode("category");
        txt += attnode.name + " = " + attnode.value + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

The output of the code above will be:

category = cooking
category = children
category = web
category = web
Try it Yourself »

Definition and Usage

The getAttributeNode() method gets an attribute node by name from the current element.

Syntax

elementNode.getAttributeNode(name)

Parameter Description
name Required. Specifies the attribute node to get

Element Object Reference Element Object