Availability
DOM Level 1 HTML
Properties
readonly unsigned long length
The number
of elements in the collection.
Methods
item( )
Returns the element at the specified position in the collection. You
can also simply specify the position within array brackets instead of
calling this method explicitly.
namedItem( )
Returns the element from the collection that has the specified value
for its id or name attribute,
or null if there is no such element. You may also
place the element name within array brackets instead of calling this
method explicitly.
Description
An HTMLCollection is a collection of HTML elements with methods that
allow you to retrieve the elements by their position in the document
or by their id or name
attribute. In JavaScript, HTMLCollection objects behave like
read-only arrays, and you may use JavaScript square-bracket notation
to index an HTMLCollection by number or by name instead of calling
the item( ) and namedItem( )
methods.
A number of the properties of the HTMLDocument interface (which
standardizes the DOM Level 0 Document object) are HTMLCollection
objects, which provide convenient access to document elements such as
forms, images, and links. The HTMLCollection object also provides a
convenient way to traverse the elements of an HTML form, the rows of
an HTML table, the cells of a table row, and the areas of a
client-side image map.
HTMLCollection objects are read-only: you cannot assign new elements
to them, even when using JavaScript array notation. They are
"live," meaning that if the underlying document changes,
those changes are immediately visible through all HTMLCollection
objects.
Example
var c = document.forms; // This is an HTMLCollection of form elements
var firstform = c[0]; // It can be used like a numeric array
var lastform = c[c.length-1]; // The length property gives the number of elements
var address = c["address"]; // It can be used like an associative array
var address = c.address; // JavaScript allows this notation, too
See Also
NodeList
Type of
HTMLDocument.anchors, HTMLDocument.applets, HTMLDocument.forms,HTMLDocument.images, HTMLDocument.links, HTMLFormElement.elements,HTMLMapElement.areas, HTMLSelectElement.options,HTMLTableElement.rows, HTMLTableElement.tBodies,HTMLTableRowElement.cells, HTMLTableSectionElement.rows
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.