What is the preferred way to get a reference to an element in a document?

R

Rosinger

Hi all,

What is the preferred in term of browser copatability to get a
reference to an element in the document in JavaScript?

I am not sure about when to use document.all or document.layers or
document.getElementByID. Are they all doing the same thing?

Cheers,

dave
 
D

David Dorward

Rosinger said:
What is the preferred in term of browser copatability to get a
reference to an element in the document in JavaScript?
I am not sure about when to use document.all or document.layers or
document.getElementByID. Are they all doing the same thing?

Similar things.

The _standard_ way is document.getElementById, but not all browsers support
that. You could use object detection though, possibly something like:

var x;
if (document.getElementById) {
x = document.getElementById('foo');
} else if (document.all) {
...
} else if (document.layers) {
...
}

(Provisio - I'm not quite awake yet, I think the structure is right, but its
quite likely there are syntax errors in there.)
 
L

Lasse Reichstein Nielsen

I am not sure about when to use document.all or document.layers or
document.getElementByID. Are they all doing the same thing?

The preferred way is "document.getElementById". It is from the W3C DOM
specification.

In IE 4 and earlier, that function is not available, so you have to use
the Microsoft proprietary collection "document.all". Apart from being
a collection (so you access it as "document.all[id]" instead of calling
it as a function), it does the same thing as "document.getElementById".

In Netscape 4 and earlier, neither of these two are available. In fact,
there is no way to access an arbitrary element of a page. You can only
access elements through the available collections. Those are
document.images (<img> elements)
document.links (<a href...> and <area href...> elements)
document.ancors (<a name...> elements)
document.forms (<form> elements)
document.applets (<applet> applets)
document.embeds (<embed> elements)
document.layers (<layer> elements and elements with CSS position:absolute)
and
document.forms[...].elements (form elements in a form)

Most of these Netscape collections are also valuid W3C DOM, however
"document.layers" is not. If you are dealing with images, links, or
forms and their elements, you might as well use the collection.

/L
 
V

Vladdy

| Hi all,
|
| What is the preferred in term of browser copatability to get a
| reference to an element in the document in JavaScript?
|
| I am not sure about when to use document.all or document.layers or
| document.getElementByID. Are they all doing the same thing?
|
| Cheers,
|
| dave

The correct way is using document.getElementById
The browsers that do not support it are better off without javascript
anyway.

Vladdy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,569
Members
47,206
Latest member
MalorieSte

Latest Threads

Top