THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

XML DOM getAttribute() Method


Element Object Reference Element Object

Example

The following code fragment loads "books.xml" into xmlDoc and gets the value of the "category" attribute in 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, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for (i = 0; i < x.length; i++) {
        txt += x[i].getAttribute('category') + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

The output of the code above will be:

cooking
children
web
web
Try it Yourself »

Definition and Usage

The getAttribute() method gets an attribute value by name.

Syntax

elementNode.getAttribute(name)

Parameter Description
name Required. Specifies the attribute to get the attribute value from

Element Object Reference Element Object