11 out of 89 rated this helpful - Rate this topic

all property

[This documentation is preliminary and is subject to change.]

Returns a reference to the collection of elements contained by the object.

Syntax

JavaScript

p = object.all

Property values

Type: IDispatch

Array of elements contained by the object.

Standards information

There are no standards that apply here.

Remarks

The all collection includes one element object for each valid HTML tag. If a valid tag has a matching end tag, both tags are represented by the same element object.

The collection returned by the document's all collection always includes a reference to the HTML, HEAD, and TITLE objects regardless of whether the tags are present in the document. If the BODY tag is not present, but other HTML tags are, a BODY object is added to the all collection.

If the document contains invalid or unknown tags, the collection includes one element object for each. Unlike valid end tags, unknown end tags are represented by their own element objects. The order of the element objects is the HTML source order. Although the collection indicates the order of tags, it does not indicate hierarchy.

The name property only applies to some elements such as form elements. If the vIndex is set to a string matching the value of a name property in an element that the name property does not apply, then that element will not be added to the collection.

Examples

This example, in JScript (compatible with ECMA 262 language specification), shows how to display the names of all tags in the document in the order the tags appear in the document.


for(i = 0; i < document.all.length; i++){
   alert(document.all(i).tagName);
}

This example, also in JScript, shows how to use the item method on the all collection to retrieve all element objects for which the name property or ID attribute is set to sample. Depending on the number of times the name or ID is defined in the document, the item method returns null, a single element object, or a collection of element objects. The value of the length property of the collection determines whether item returns a collection or a single object.


var oObject = document.all.item("sample");
if (oObject != null){
   if (oObject.length != null){
      for (i = 0; i < oObject.length; i++){
         alert(oObject(i).tagName);
      }
   }
   else{
      alert(oObject.tagName);
   }
} 

 

 

Build date: 3/8/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Use W3C DOM getElementsByTagName to avoid Firefox warnings

Although the browsers Firefox, Safari & Opera implement the document.all collection, this is not a Web standard (W3C DOM) collection, so Firefox generates a warning everytime you use it (in the Error Console).

As there is a direct W3C DOM equivalent, you might as well use it:

var elementCollection = document.getElementsByTagName("*");

// Example usage
for (var i = 0; i < elementCollection.length; i++)
{
var element = elementCollection[i];

// ...
}

This collection has the same number of elements as document.all, infact typically you aren't interested in the top-level elements, such as HTML & HEAD, so you could just retrieve the elements in the BODY element instead:

var elementCollection = document.body.getElementsByTagName("*");
This is NOT a live query!
This is only a collection of the elements as the page was loaded. any changes after the load by JavaScript are NOT included in the collection.
This collection is not a collection IF...

This collection doesn't work if we have one child element. Assume we have two checkboxes in a div named="theDiv" and <input type="checkbox" name="chk"/><input type="checkbox" name="chk"/> and wrote:

function getCount(theDiv) { 
    var a = theDiv.all("chk") 
    alert(a.length) 
} 

It will alert 2, but if we have only 1 checkbox return undefined.

[jsudds.msft] Don't forget that there are three possible return values from this collection.

  • null - not found
  • element - 1 match
  • array - 2 or more

Refer to the code sample above for a working example.