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

XML DOM item() Method


NamedNodeMap Object Reference NamedNodeMap Object

Example

The following code fragment loads "books.xml" into xmlDoc, loops through the <book> elements and prints the values of the category attributes:

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, att, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for (i = 0; i < x.length; i++) {
        att = x.item(i).attributes.getNamedItem("category");
        txt += att.value + "<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 item() method returns the node at the specified index in a node list.

Syntax

item(index)

Parameter Description
index The index

Try-It-Yourself Demos

item() - Loop through the items in a node list

getNamedItem() - Change an item's value


NamedNodeMap Object Reference NamedNodeMap Object