NodeIterator

  • Revision slug: Web/API/NodeIterator
  • Revision title: NodeIterator
  • Revision id: 452167
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef() }}

The NodeIterator interface represents an iterator over the members of a list of the nodes in a subtree of the DOM. The nodes will be returned in document order.

A NodeIterator can be created using the {{domxref("Document.createNodeIterator()")}} method, as follows:

var nodeIterator = document.createNodeIterator(root, whatToShow, filter);

Properties

This interface doesn't inherit any property.

{{domxref("NodeIterator.root")}} {{readonlyInline}}
Returns a {{domxref("Node")}} representing the root node as specified when the NodeIterator was created.
{{domxref("NodeIterator.whatToShow")}} {{readonlyInline}}
Returns an unsigned long being a bitmask made of constants describing the types of {{domxref("Node")}} that must to be presented. Non-matching nodes are skipped, but their children may be included, if relevant. The possible values are:
Constant Numerical value Description
NodeFilter.SHOW_ALL -1 (that is the max value of unsigned long) Shows all nodes.
NodeFilter.SHOW_ATTRIBUTE {{deprecated_inline}} 2 Shows attribute {{ domxref("Attr") }} nodes. This is meaningful only when creating a {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} with an {{ domxref("Attr") }} node as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. Since attributes are never children of other nodes, they do not appear when traversing over the document tree.
NodeFilter.SHOW_CDATA_SECTION {{deprecated_inline}} 8 Shows {{ domxref("CDATASection") }} nodes.
NodeFilter.SHOW_COMMENT 128 Shows {{ domxref("Comment") }} nodes.
NodeFilter.SHOW_DOCUMENT 256 Shows {{ domxref("Document") }} nodes.
NodeFilter.SHOW_DOCUMENT_FRAGMENT 1024 Shows {{ domxref("DocumentFragment") }} nodes.
NodeFilter.SHOW_DOCUMENT_TYPE 512 Shows {{ domxref("DocumentType") }} nodes.
NodeFilter.SHOW_ELEMENT 1 Shows {{ domxref("Element") }} nodes.
NodeFilter.SHOW_ENTITY {{deprecated_inline}} 32 Shows {{ domxref("Entity") }} nodes. This is meaningful only when creating a {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} with an {{ domxref("Entity") }} node as its root; in this case, it means that the {{ domxref("Entity") }} node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.
NodeFilter.SHOW_ENTITY_REFERENCE {{deprecated_inline}} 16 Shows {{ domxref("EntityReference") }} nodes.
NodeFilter.SHOW_NOTATION {{deprecated_inline}} 2048 Shows {{ domxref("Notation") }} nodes. This is meaningful only when creating a {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} with a {{ domxref("Notation") }} node as its root; in this case, it means that the {{ domxref("Notation") }} node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.
NodeFilter.SHOW_PROCESSING_INSTRUCTION 64 Shows {{ domxref("ProcessingInstruction") }} nodes.
NodeFilter.SHOW_TEXT 4 Shows {{ domxref("Text") }} nodes.
{{domxref("NodeIterator.filter")}} {{readonlyInline}}
Returns a {{domxref("NodeFilter")}} used to select the relevant nodes.
{{domxref("NodeIterator.expandEntityReferences")}} {{readonlyInline}} {{obsolete_inline}}
Is a {{domxref("Boolean")}} indicating if, when discarding an {{domxref("EntityReference")}} its whole sub-tree must be discarded at the same time.
{{domxref("NodeIterator.referenceNode")}} {{readonlyInline}} {{experimental_inline() }}
Returns the {{domxref("Node")}} to which the iterator is anchored.
{{domxref("NodeIterator.pointerBeforeReferenceNode")}} {{readonlyInline}} {{ experimental_inline() }}
Returns a {{domxref("Boolean")}} flag that indicates whether the {{domxref("NodeFilter")}} is anchored before, the flag being true, or after, the flag being false, the anchor node.

Methods

This interface doesn't inherit any method.

{{domxref("NodeIterator.detach()")}} {{deprecated_inline()}}
This operation is a no-op. It doesn't do anything. Previously it was telling the engine that the NodeIterator was no more used, but this is now useless.
{{domxref("NodeIterator.previousNode()")}}
Returns the previous {{domxref("Node")}} in the document, or null if there are none.
{{domxref("NodeIterator.nextNode()")}}
Returns the next {{domxref("Node")}} in the document, or null if there are none.

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#nodeiterator', 'NodeIterator')}} {{Spec2('DOM WHATWG')}} Added the referenceNode and pointerBeforeReferenceNode properties.
Removed the expandEntityReferences property.
The method detach() has been changed to be a no-op.
The methods previousNode() and nextNode() don't raise an exception any more.
{{SpecName('DOM2 Traversal_Range', 'traversal.html#Iterator-overview', 'NodeIterator')}} {{Spec2('DOM2 Traversal_Range')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.9.1")}} 9.0 9.0 3.0
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("1.9.1")}} {{CompatVersionUnknown}} 9.0 3.0

See also

  • The creator method: {{domxref("Document.createNodeIterator()")}}.
  • Related interfaces: {{domxref("NodeFilter")}}, {{domxref("TreeWalker")}}.

Revision Source

<p>{{ DomRef() }}</p>
<p>The <strong><code>NodeIterator</code></strong> interface represents an iterator over the members of a list of the nodes in a subtree of the DOM. The nodes will be returned in document order.</p>
<p>A <code>NodeIterator</code> can be created using the {{domxref("Document.createNodeIterator()")}} method, as follows:</p>
<pre class="brush: js">
var nodeIterator = document.createNodeIterator(<em>root</em>, <em>whatToShow</em>, <em>filter</em>);</pre>
<h2 id="Properties" name="Properties">Properties</h2>
<p><em>This interface doesn't inherit any property.</em></p>
<dl>
  <dt>
    {{domxref("NodeIterator.root")}} {{readonlyInline}}</dt>
  <dd>
    Returns a {{domxref("Node")}} representing the root node as specified when the <code>NodeIterator</code> was created.</dd>
  <dt>
    {{domxref("NodeIterator.whatToShow")}} {{readonlyInline}}</dt>
  <dd>
    Returns an <code>unsigned long</code> being a bitmask made of constants describing the types of {{domxref("Node")}} that must to be presented. Non-matching nodes are skipped, but their children may be included, if relevant. The possible values are:<br />
    <table class="standard-table">
      <tbody>
        <tr>
          <td class="header">Constant</td>
          <td class="header">Numerical value</td>
          <td class="header">Description</td>
        </tr>
        <tr>
          <td><code>NodeFilter.SHOW_ALL</code></td>
          <td><code>-1</code> (that is the max value of <code>unsigned long</code>)</td>
          <td>Shows all nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_ATTRIBUTE</code> {{deprecated_inline}}</td>
          <td><code>2</code></td>
          <td>Shows attribute {{ domxref("Attr") }} nodes. This is meaningful only when creating a {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} with an {{ domxref("Attr") }} node as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. Since attributes are never children of other nodes, they do not appear when traversing over the document tree.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_CDATA_SECTION</code> {{deprecated_inline}}</td>
          <td><code>8</code></td>
          <td>Shows {{ domxref("CDATASection") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_COMMENT</code></td>
          <td><code>128</code></td>
          <td>Shows {{ domxref("Comment") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_DOCUMENT</code></td>
          <td><code>256</code></td>
          <td>Shows {{ domxref("Document") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_DOCUMENT_FRAGMENT</code></td>
          <td><code>1024</code></td>
          <td>Shows {{ domxref("DocumentFragment") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_DOCUMENT_TYPE</code></td>
          <td><code>512</code></td>
          <td>Shows {{ domxref("DocumentType") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_ELEMENT</code></td>
          <td><code>1</code></td>
          <td>Shows {{ domxref("Element") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_ENTITY</code> {{deprecated_inline}}</td>
          <td><code>32</code></td>
          <td>Shows {{ domxref("Entity") }}&nbsp;nodes. This is meaningful only when creating a {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} with an {{ domxref("Entity") }} node as its root; in this case, it means that the {{ domxref("Entity") }} node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_ENTITY_REFERENCE</code> {{deprecated_inline}}</td>
          <td><code>16</code></td>
          <td>Shows {{ domxref("EntityReference") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code><code>NodeFilter.</code></code>SHOW_NOTATION</code> {{deprecated_inline}}</td>
          <td><code>2048</code></td>
          <td>Shows {{ domxref("Notation") }} nodes. This is meaningful only when creating a {{ domxref("NodeIterator") }} or {{ domxref("TreeWalker") }} with a {{ domxref("Notation") }} node as its root; in this case, it means that the {{ domxref("Notation") }} node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_PROCESSING_INSTRUCTION</code></td>
          <td><code>64</code></td>
          <td>Shows {{ domxref("ProcessingInstruction") }}&nbsp;nodes.</td>
        </tr>
        <tr>
          <td><code><code>NodeFilter.</code>SHOW_TEXT</code></td>
          <td><code>4</code></td>
          <td>Shows {{ domxref("Text") }}&nbsp;nodes.</td>
        </tr>
      </tbody>
    </table>
  </dd>
  <dt>
    {{domxref("NodeIterator.filter")}} {{readonlyInline}}</dt>
  <dd>
    Returns a {{domxref("NodeFilter")}} used to select the relevant nodes.</dd>
  <dt>
    {{domxref("NodeIterator.expandEntityReferences")}} {{readonlyInline}} {{obsolete_inline}}</dt>
  <dd>
    Is a {{domxref("Boolean")}} indicating if, when discarding an {{domxref("EntityReference")}} its whole sub-tree must be discarded at the same time.</dd>
  <dt>
    {{domxref("NodeIterator.referenceNode")}} {{readonlyInline}} {{experimental_inline() }}</dt>
  <dd>
    Returns the {{domxref("Node")}} to which the iterator is anchored.</dd>
  <dt>
    {{domxref("NodeIterator.pointerBeforeReferenceNode")}} {{readonlyInline}} {{ experimental_inline() }}</dt>
  <dd>
    Returns a {{domxref("Boolean")}} flag that indicates whether the {{domxref("NodeFilter")}} is anchored before, the flag being <code>true</code>, or after, the flag being <code>false</code>, the anchor node.</dd>
</dl>
<h2 id="Methods" name="Methods">Methods</h2>
<p><em>This interface doesn't inherit any method.</em></p>
<dl>
  <dt>
    {{domxref("NodeIterator.detach()")}} {{deprecated_inline()}}</dt>
  <dd>
    This operation is a no-op. It doesn't do anything. Previously it was telling the engine that the <code>NodeIterator</code> was no more used, but this is now useless.</dd>
  <dt>
    {{domxref("NodeIterator.previousNode()")}}</dt>
  <dd>
    Returns the previous {{domxref("Node")}} in the document, or <code>null</code> if there are none.</dd>
  <dt>
    {{domxref("NodeIterator.nextNode()")}}</dt>
  <dd>
    Returns the next {{domxref("Node")}} in the document, or <code>null</code> if there are none.</dd>
</dl>
<h2 id="Specification" name="Specification">Specifications</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
    <tr>
      <td>{{SpecName('DOM WHATWG', '#nodeiterator', 'NodeIterator')}}</td>
      <td>{{Spec2('DOM WHATWG')}}</td>
      <td>Added the <code>referenceNode</code> and <code>pointerBeforeReferenceNode</code> properties.<br />
        Removed the <code>expandEntityReferences</code> property.<br />
        The method <code>detach()</code> has been changed to be a no-op.<br />
        The methods <code>previousNode()</code> and <code>nextNode()</code> don't raise an exception any more.</td>
    </tr>
    <tr>
      <td>{{SpecName('DOM2 Traversal_Range', 'traversal.html#Iterator-overview', 'NodeIterator')}}</td>
      <td>{{Spec2('DOM2 Traversal_Range')}}</td>
      <td>Initial definition.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.9.1")}}</td>
        <td>9.0</td>
        <td>9.0</td>
        <td>3.0</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoMobile("1.9.1")}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>9.0</td>
        <td>3.0</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li>The creator method: {{domxref("Document.createNodeIterator()")}}.</li>
  <li>Related interfaces: {{domxref("NodeFilter")}}, {{domxref("TreeWalker")}}.</li>
</ul>
Revert to this revision