Easy Tutorial
❮ Met Element Getattributens Dom Met Node Hasattributes ❯

XML DOM item() Method



Definition and Usage

The item() method returns the node at the specified index in a node list.

Syntax

Parameter Description
index The index.

Tips and Notes

Note: Firefox and most other browsers treat spaces or newlines between nodes as text nodes, while Internet Explorer ignores whitespace text nodes generated between nodes. Therefore, in the example below, we will use a function to check the type of node.

The node type for element nodes is 1. Thus, if the child node is not an element node, it moves to the next node and continues checking if this node is an element node. This method ensures correct results across all browsers.

Tip: For more information on browser differences, visit our DOM Browsers section in the XML DOM tutorial.


Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc and loops through all child elements of the <bookstore> element:

Example

xmlDoc = loadXMLDoc("books.xml");

x = xmlDoc.documentElement.childNodes;

for (i = 0; i < x.length; i++) {
  // Display element node name
  if (x.item(i).nodeType == 1) {
    document.write(x.item(i).nodeName);
    document.write("<br>");
  }
}

Output:

book
book
book
book

❮ Met Element Getattributens Dom Met Node Hasattributes ❯