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

Discussion in 'Javascript' started by seans, Aug 14, 2005.

  1. seans

    seans Guest

    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
    seans, Aug 14, 2005
    #1
    1. Advertising

  2. seans

    RobG Guest

    seans wrote:
    > 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>

    [...]

    --
    Rob
    RobG, Aug 14, 2005
    #2
    1. Advertising

  3. seans

    seans Guest

    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
    seans, Aug 14, 2005
    #3
  4. 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

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Aug 14, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs

    document.tagID vs document.all.tagID

    chirs, Dec 1, 2003, in forum: Javascript
    Replies:
    1
    Views:
    66
    Lasse Reichstein Nielsen
    Dec 1, 2003
  2. Jim Ley
    Replies:
    3
    Views:
    197
  3. wk
    Replies:
    5
    Views:
    161
    Dietmar Meier
    Apr 22, 2005
  4. Martin
    Replies:
    3
    Views:
    136
  5. yawnmoth

    document.all vs. document.layers

    yawnmoth, Dec 24, 2005, in forum: Javascript
    Replies:
    2
    Views:
    82
    Michael Winter
    Dec 24, 2005
Loading...

Share This Page