Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.
L'interface HTMLCollection est constituée d'une collection générique (à la manière d'un tableau) d'éléments (respectant l'ordre du document) et offre des méthodes et des propriétés pour sélectionner ces éléments dans la liste.
Une HTMLCollection dans le DOM HTML est automatiquement mise à jour quand le document concerné change.
Properties
HTMLCollection.lengthLecture seule- Returns the number of items in the collection.
Methods
HTMLCollection.item()- Returns the specific node at the given zero-based
indexinto the list. Returnsnullif theindexis out of range. HTMLCollection.namedItem()- 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 thenameattribute. Returnsnullif no node exists by the given name.
Usage in JavaScript
In JavaScript, code that accesses HTMLCollection object, in order to get an item of the given HTMLCollection, the square bracket syntax can be used 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 NodeList of all matching elements.

