You are here: Reference > JavaScript > client-side > xml handling > methods > snapshotItem (XPathResult)

snapshotItem method (XPathResult)

Browser support:
Returns a matching node from the snapshots collection.
The XPathResult object represents the result of the evaluate method. The type of the result depends on the fourth parameter of the evaluate method, or if its value is ANY_TYPE, then it depends on the expression specified by the first parameter.
With the resultType property, the type of the result can be retrieved. If the value of the resultType property is UNORDERED_NODE_SNAPSHOT_TYPE or ORDERED_NODE_SNAPSHOT_TYPE, then the result contains snapshots for all nodes that match the expression. In this case, the snapshotItem method can be used to retrieve the matching nodes from the snapshots collection, by position. Use the snapshotLength property to get the length of the snapshots collection.
The snapshotItem method is similar to the iterateNext method. Both provide access to the matching nodes. The main difference is that the modification of the document invalidates the iteration, but does not invalidate the snapshots collection.


object.snapshotItem (index);
You can find the related objects in the Supported by objects section below.


Required. A zero-based integer that specifies the position of the matching node.

Return value:

Returns the node at the specified position. If the value of the index parameter is negative or greater than or equal to the number of nodes in the snapshots collection, it returns null.

Example HTML code 1:

This example shows how to get all span elements that are immediate children of a given element:
        .level1 {color:red;}
        .level2 {color:blue;}
        .level3 {color:green;}
    <script type="text/javascript">
        function GetSpanElements () {
            var message = "";

            if (document.evaluate) {    // Firefox, Opera, Google Chrome and Safari
                var container = document.getElementById ("container");
                var xPathRes = document.evaluate ( 'span', container, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

                message += xPathRes.snapshotLength + " span elements are found.<br />";
                for (var i = 0; i < xPathRes.snapshotLength; i++) {
                    var actualSpan = xPathRes.snapshotItem (i);
                    message += "The contents of the " + i + ". child span element: <b>" + actualSpan.innerHTML + "</b><br />";
            else {  // Internet Explorer
                message = "Your browser does not support the evaluate method!";

            var info = document.getElementById ("info");
            info.innerHTML = message;
<body onload="GetSpanElements ()">
    <span class="level1">Span 1</span>
    <div id="container">
        <span class="level2">Span 2</span>
            <span class="level3">Span 3</span>
            <span class="level3">Span 4</span>
        <span class="level2">Span 5</span>
        <span class="level2">Span 6</span>
    <span class="level1">Span 7</span>
    <br /><br />
    <div id="info" style="background-color:#e0b0b0; width:400px"></div>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content