XML DOM getAttributeNode() Method


❮ 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 (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
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
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.