How do I get the object type from document.all?

S

seans

Hi,

Please, can somebody tell me how to do this?

I am using document.all in a script to retrieve the elements in a
document which have a certain id. document.all returns a reference to
an element or an array. If the id is unique within the document then
document.all returns a reference to the element. If there is more than
one element with the same id then an array is returned.

Is there some way of determining what type of object is returned? I can
look at the properties of the object but is there some other way of
finding out the object type?

Thanks in advance.

sean
 
R

RobG

seans said:
Hi,

Please, can somebody tell me how to do this?

I am using document.all in a script to retrieve the elements in a
document which have a certain id. document.all returns a reference to
an element or an array. If the id is unique within the document then
document.all returns a reference to the element. If there is more than
one element with the same id then an array is returned.

Having multiple instances of the same ID will make your HTML invalid.
ID's must be unique within a page.

And unless your page is for IE only, use DOM compliant methods and
feature detection:

<URL:http://www.jibbering.com/faq/#FAQ4_15>

[...]
 
S

seans

Hi Rob.

Thanks for your help. This seems to work.

if (document.all['elementID'].len­gth) { // we have an array

}
else { // we have a single object

}

cheers,

sean
 
M

Michael Winter

On 14/08/2005 13:06, seans wrote:

[snip]
if (document.all['elementID'].len­gth) { // we have an array

}
else { // we have a single object

}

Except where the element has its own length property, as in the FORM and
SELECT elements. Perhaps a better test is to check for the tags method,
which should only exist on all-based collections.

Still, the all collection should, in my opinion, only be used in two
circumstances:

1) When writing code for IE 4.
2) When fixing-up the getElementsByTagName method in IE 5.x and
earlier, when its argument is an asterisk ('*').

In other circumstances, it should be abandoned in favour of the
getElementById method.

Mike
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top