Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 640397 of HTMLCollection

  • Revision slug: Web/API/HTMLCollection
  • Revision title: HTMLCollection
  • Revision id: 640397
  • Created:
  • Creator: evinugur
  • Is current revision? No
  • Comment

Revision Content

HTMLCollection is an interface representing a generic collection (array) of elements (in document order) and offers methods and properties for selecting from the list.

Note: This interface is called HTMLCollection for historical reasons (before DOM4, collections implementing this interface could only have HTML elements as their items).

HTMLCollections in the HTML DOM are live; they are automatically updated when the underlying document is changed.

Properties

HTMLCollection.length {{readonlyInline}}
The number of items in the collection.

Methods

HTMLCollection.item(index)
Returns the specific node at the given zero-based index into the list. Returns null if the index is out of range.
HTMLCollection.namedItem(name)
Returns the specific node whose ID or, as a fallback, name matches the string specified by name. Matching by name is only done as a last resort, only in HTML, and only if the referenced element supports the name attribute. Returns null if no node exists by the given name.

Usage in JavaScript

In JavaScript code that accesses HTMLCollections, in order to get an item of the given HTMLCollection, you can use the square bracket syntax instead of directly calling the item() or namedItem() methods. Numeric values in the square brackets will work the same way as item(), string values will work the same way as namedItem().

For example, assuming there is one <form> element in the document and its id is "myForm":

var elem1, elem2;

// document.forms is an HTMLCollection

elem1 = document.forms[0];
elem2 = document.forms.item(0);

alert(elem1 === elem2); // shows: "true"

elem1 = document.forms["myForm"];
elem2 = document.forms.namedItem("myForm");

alert(elem1 === elem2); // shows: "true"

Browser compatibility

Different browsers behave differently when there are more than one elements matching the string used as an index (or namedItem's argument). Firefox 8 behaves as specified in DOM 2 and DOM4, returning the first matching element. WebKit browsers and Internet Explorer in this case return another HTMLCollection and Opera returns a {{domxref("NodeList")}} of all matching elements.

Specification

See also

  • {{domxref("NodeList")}}
  • {{domxref("HTMLOptionsCollection")}}

Revision Source

<p><code>HTMLCollection</code> is an interface representing a generic collection (array) of elements (in document order) and offers methods and properties for selecting from the list.</p>
<div class="note">
 <strong>Note:</strong> This interface is called <code>HTMLCollection</code> for historical reasons (before DOM4, collections implementing this interface could only have HTML elements as their items).</div>
<p><code>HTMLCollections</code>&nbsp;in the HTML DOM are live; they are automatically updated when the underlying document is changed.</p>
<h2 id="Properties">Properties</h2>
<dl>
 <dt>
  <code>HTMLCollection.length</code> {{readonlyInline}}</dt>
 <dd>
  The number of items in the collection.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
 <dt>
  <code><code>HTMLCollection.</code>item(index)</code></dt>
 <dd>
  Returns the specific node at the given zero-based <code>index</code> into the list. Returns <code>null</code> if the <code>index</code> is out of range.</dd>
 <dt>
  <code><code>HTMLCollection.</code>namedItem(name)</code></dt>
 <dd>
  Returns the specific node whose ID or, as a fallback, name matches the string specified by <code>name</code>. Matching by name is only done as a last resort, only in HTML, and only if the referenced element supports the <code>name</code> attribute. Returns <code>null</code> if no node exists by the given name.</dd>
</dl>
<h2 id="Usage_in_JavaScript">Usage in JavaScript</h2>
<p>In JavaScript code that accesses HTMLCollections, in order to get an item of the given HTMLCollection, you can use the square bracket syntax instead of directly calling the <code>item()</code> or <code>namedItem()</code> methods. Numeric values in the square brackets will work the same way as <code>item()</code>, string values will work the same way as <code>namedItem().</code></p>
<p>For example, assuming there is one <code>&lt;form&gt;</code> element in the document and its <code>id</code> is <code>"myForm"</code>:</p>
<pre class="brush:js">
var elem1, elem2;

// document.forms is an HTMLCollection

elem1 = document.forms[0];
elem2 = document.forms.item(0);

alert(elem1 === elem2); // shows: "true"

elem1 = document.forms["myForm"];
elem2 = document.forms.namedItem("myForm");

alert(elem1 === elem2); // shows: "true"</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>Different browsers behave differently when there are more than one elements matching the string used as an index (or <code>namedItem</code>'s argument). Firefox 8 behaves as specified in DOM 2 and DOM4, returning the first matching element. WebKit browsers and Internet Explorer in this case return another <code>HTMLCollection</code> and Opera returns a {{domxref("NodeList")}} of all matching elements.</p>
<h2 id="Specification">Specification</h2>
<ul>
 <li><a href="https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75708506" title="https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75708506">DOM Level 2 HTML, Section 1.4, Miscellaneous Object Definitions</a></li>
 <li><a href="https://www.w3.org/TR/domcore/#interface-htmlcollection" title="https://www.w3.org/TR/domcore/#interface-htmlcollection">DOM4: HTMLCollection</a></li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
 <li>{{domxref("NodeList")}}</li>
 <li>{{domxref("HTMLOptionsCollection")}}</li>
</ul>
Revert to this revision