Note : The All collection is Internet Explorer 4.0 specific. As Netscape doesn't support scripting for every HTML element, it doesn't support the All collection.
The All collection is an ordered, indexed array, containing a reference to every opening HTML element in a document. For example, consider the following document:
<HTML>
<HEAD>
<TITLE>Test Document</TITLE>
</HEAD>
<BODY>
<P>This is a test
<HR>
</BODY>
</HTML>
The all collection would be indexed from 0 to 5, with the elements being positioned:
Element | All index |
<HTML> |
0 |
<HEAD> |
1 |
<TITLE> |
2 |
<BODY> |
3 |
<P> |
4 |
<HR> |
5 |
Element Object would normally be retrieved by their index in the All collection (for example, above document.all(3)
contains a reference to the <BODY>
element), but a string value can be used, as long as that string is a valid identifier (ID
attribute value) for an element in the document.
E.g., changing the above example to:
<HTML>
<HEAD>
<TITLE ID="Title1">Test Document</TITLE>
</HEAD>
<BODY>
<P>This is a test
<HR>
</BODY>
</HTML>
...would mean that document.all('Title1')
would be a reference to the <TITLE>
element (whose ID
is 'Title1'). This would be the same as document.all(2)
. Note that it is also equivalent to using document.all.item('Title1')
(see the item method below).
Note : The All collection always includes a reference to <HTML>
, <HEAD>
and <BODY>
elements, whether they exist in the document or not.
length
The length
property returns the number of elements in the collection. Note that the length
count starts at 1, not 0 as the all collection index does. Therefore, the length
property may return a value of 5, but to access the 3rd element, you'd need to use document.all(2).property
item
The item
method retrieves single items, or sub-collections from the all collection. It accepts the following arguments:
all.item(index, sub-index)
If index
is a number, then the method returns a reference to the element object at that position in the all collections index. I.e. (using the example above)
strTag=document.all.item(2).tagName
would make strTag
be TITLE
- the value of the <TITLE>
elements tagName
property. As you can see, this is effectively the long-hand version of using document.all(2).property
.
If the index
property is a string value, then the item
method returns a sub-collection, containing a reference to every element in the document that has its ID
or NAME
attribute set to the string contained in the index
argument. To retrieve certain element objects from this sub-collection, the sub-index
argument must be used. For example, assume that 5 elements in a document all have the NAME
attribute set as radColour
(these imaginary elements are radio buttons). To interrogate the 3rd of these elements, you could use:
strThird=document.all('radColour',2).propertyName
tags
The tags
method returns a collection of element objects whose tagName
property is the same as the tag
argument used for the method. This differs from the item
property in that that interrogates ID
and NAME
values if necessary.
document.all.tags('EM')
would return a collection of all the <EM>
objects in the document.
© 1995-1998, Stephen Le Hunte
Comments:
Questions: