You are here: Reference > JavaScript > client-side > xml handling > properties > invalidIteratorState (XPathResult)

invalidIteratorState property (XPathResult)

Browser support:
Retrieves whether the iterator of the current XPathResult object is valid or not.
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, 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_ITERATOR_TYPE or ORDERED_NODE_ITERATOR_TYPE, then the iterateNext method can be used to retrieve the matching nodes from the result set. In that case, if the document is modified during the iteration, the value of the invalidIteratorState property becomes true. A call to the iterateNext method raises an exception when the iterator is invalid.


You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Boolean that specifies whether the iterator is valid or not.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the invalidIteratorState property:
        .level1 {color:red;}
        .level2 {color:blue;}
        .level3 {color:green;}
    <script type="text/javascript">
        function TestIterator () {
            if (document.evaluate) {    // Firefox, Opera, Google Chrome and Safari
                var container = document.getElementById ("container");
                var xPathRes = document.evaluate ( 'span', container, null, XPathResult.ANY_TYPE, null);

                var idx = 1;
                var actualSpan = xPathRes.iterateNext ();
                while (actualSpan) {
                    alert ("The " + idx + ". result: " + actualSpan.textContent);

                    if (idx == 2) {
                            // Modify the contents of the container element
                        var firstSpan = container.getElementsByTagName ("span")[0];
                        container.removeChild (firstSpan);
                    if (xPathRes.invalidIteratorState) {
                        alert ("The iterator has become invalid, iteration stopped!");

                    actualSpan = xPathRes.iterateNext ();
            else {  // Internet Explorer
                alert ("Your browser does not support the evaluate method!");
    <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 />
    <button onclick="TestIterator ()">Test iterator state!</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content