Behaviour of HTML collection classes

HTMLCollection

The following summarises the behaviour of HTMLCollection objects in browsers (looking specifically at HTMLDocument.links, HTMLDocument.images, HTMLDocument.embeds, HTMLDocument.forms, HTLMDocument.scripts and HTMLTableElement.rows).

IE8b2 Fx3.0.4 Op9.6 Saf3.2 Cr0.3
An index property exists for each matching element yes yes yes yes yes
A name property exists for an element with a "normal" name yes3 yes yes yes yes
A name property exists for an element with a name that looks like an index no7 no5 no yes8 no
Index properties are enumerable no4 yes yes yes yes
Name properties are enumerable yes1 no yes1 no no
Getting via a name property that matches multiple elements returns a single element no2 yes no2 no no
Getting via a name property that conflicts with an HTMLCollection operation returns the matching element yes no yes no no
Getting via a name property that conflicts with an HTMLCollection attribute returns the matching element yes no no6 no no
Getting via a name property that conflicts with a property on the Object prototype object returns the matching element yes no yes no no
Collection object [[HasProperty]] index property yes yes yes yes yes
Index property is on the collection object yes yes no yes yes
Collection object [[HasProperty]] name property yes no yes yes yes
Name property is on the collection object yes no no yes yes

HTMLFormControlsCollection

The following summarises the behaviour of HTMLFormControlsCollection objects in browsers (just looking at HTMLFormElement.elements).

IE8b2 Fx3.0.4 Op9.6 Saf3.2 Cr0.3
An index property exists for each matching element yes yes yes yes yes
A name property exists for an element with a "normal" name yes yes yes yes yes
A name property exists for an element with a name that looks like an index no7 no5 no yes8 no
Index properties are enumerable no yes yes yes yes
Name properties are enumerable yes1 no yes1 no no
Getting via a name property that matches multiple elements returns a single element no no no no no
Getting via a name property that conflicts with an HTMLCollection operation returns the matching element yes no yes no no
Getting via a name property that conflicts with an HTMLCollection attribute returns the matching element yes no yes no no
Getting via a name property that conflicts with a property on the Object prototype object returns the matching element yes no yes no no
Collection object [[HasProperty]] index property yes yes yes yes yes
Index property is on the collection object yes yes no yes yes
Collection object [[HasProperty]] name property yes no yes yes yes
Name property is on the collection object yes no no yes yes

Notes:

  1. Opera and IE both enumerate duplicate properties when there are multiple matching elements, or when there is a matching element with the same name as one of the index or HTMLCollection interface properties (such as "length").
  2. A single element is returned for document.images.
  3. Name properties don't exist for document.links or document.anchors.
  4. Index properties are enumerable for document.links, document.anchors and document.scripts.
  5. Getting a name property that looks like a negative index throws an INDEX_SIZE_ERR DOMException.
  6. The element is returned for document.scripts.
  7. While name properties that look like indices are enumerated, the corresponding elements can't be retrieved by getting those properties (undefined is returned if the index ≥ length, and the indexed element is returned if 0 ≤ index < length). This applies to properties that look a little like index properties but shouldn't really be (like "-1", "1.0" and "+1", for example).
  8. The name property only exists where the name isn't the same as one of the index properties (i.e., when it is ≥ length).