HTML and CSS Reference
In-Depth Information
Object References in the Internet Explorer DOM
Internet Explorer supports the W3C document object model, but it also supports its own
DOM. If you work with legacy Web pages, you might encounter JavaScript code that
uses the IE DOM. One area of difference is how the IE DOM references document ele-
ments. The IE DOM supports the object reference
document.all
to create an object collection consisting of all elements within a document. To refer-
ence an element within this collection by its id, you can use either of the following
expressions:
document.all[ id ]
document.all. id
where id is the id value. The IE DOM also allows object references consisting only of an
object's id value. So to reference the element with the id mainHeading , any of the fol-
lowing object references would be supported under the IE DOM:
document.all[“mainHeading”]
document.all.mainHeading
mainHeading
The IE DOM also allows references to objects by their tag names using the tags object
collection
document.all.tags( tag )
where tag is the tag name. Note that the document.all reference and the tags collec-
tion are not part of the W3C DOM and would cause your code to fail when run by non-IE
browsers.
Creating an Array of Elements Belonging to a Common Class
Because older browsers do not support the getElementsByClassName() method, you
might have to write your own function or method to create an array of elements belong-
ing to the same class. The following code is one example of such a function:
function getElementsByCName(cValue) {
var cArray = [];
var elems = document.getElementsByTagName(*);
for (var i = 0; i < elems.length; i++) {
if (elems[i].className == cValue) cArray.push(elems[i]);
}
return cArray;
}
The function creates a variable named elems that references all of the elements in the
current document. It then loops through the object collection array, testing each element
to determine whether it belongs to the class specified in the cValue parameter. If it does,
that element is added to the cArray array using the push method. The function con-
cludes by returning the cArray array.
To create an array of elements using this function, you would call the function as in
the following example:
var group1 = getElementsByCName(“newGroup”);
Search WWH ::




Custom Search