XML DOM item() Method
Definition and Usage
item()
The method returns the node at the specified index in the node list.
Note:Access items in the node list by index, starting from 0.
Syntax
nodelist.item(index
)
) | Description |
---|---|
index | Required. Numeric. The index of the node in the node list. The index starts from 0. |
Technical Details
DOM Version: | Core Level 1 Nodelist Object |
---|---|
Return value: |
Node object. The node at the specified index. Returns null if the index is out of range. |
Example
Note:Except for Internet Explorer, all major browsers treat whitespace or new lines as text nodes. Therefore, in the following example, we use a function to check the node type and only display element nodes. This ensures that the results are the same across all browsers.
For more information on differences between browsers, please read our DOM Browser chapter.
The following code loads "books.xml" into xmlDoc and loops through all child elements of the <bookstore> element:
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, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.documentElement.childNodes; for (i = 0; i < x.length; i++) { if (x.item(i).nodeType == 1) { txt += x.item(i).nodeName + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |
All mainstream browsers support item()
Methods.