How to tell if a dropdown is defined on page

Discussion in 'Javascript' started by GIMME, Jan 8, 2004.

  1. GIMME

    GIMME Guest

    How do you tell if a drop down has been placed on a page?

    In this instance I have a form named aForm and I'm checking
    for a dropdown named div_list .


    var div_list document.aForm.div_list.options;
    if (!div_list) {}

    fails as does

    if ( null == typeof(document.aForm.div_list.options )


    Thanks.
     
    GIMME, Jan 8, 2004
    #1
    1. Advertising

  2. GIMME wrote:
    > How do you tell if a drop down has been placed on a page?
    >
    > In this instance I have a form named aForm and I'm checking
    > for a dropdown named div_list .
    >
    >


    Use the `type` property:
    document.aForm.div_list.type

    It's value is either select-one or select-multiple (the latter for a
    multiple select object).

    When you want to know the number of defined options, use:
    document.aForm.div_list.options.length


    JW
     
    Janwillem Borleffs, Jan 8, 2004
    #2
    1. Advertising

  3. (GIMME) writes:

    > How do you tell if a drop down has been placed on a page?


    I assume you mean a select element. There are many ways,
    depending on what you know about it.

    > In this instance I have a form named aForm and I'm checking
    > for a dropdown named div_list .


    Much simpler, you know the name of the form and the select.

    > var div_list document.aForm.div_list.options;


    Use
    var div_lust = document.formes['aForm'].elements['div_list'];
    This will be undefined if the element with name div_list isn't there.
    Trying to get the options property will give an error if the select
    element isn't there, so only do that after you have tested that it
    exists.

    > if (!div_list) {}


    div_list = div_list.options;

    > fails as does
    >
    > if ( null == typeof(document.aForm.div_list.options )


    Yes, you still try to find the options property of the undefined value.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jan 8, 2004
    #3
  4. Janwillem Borleffs wrote:

    > GIMME wrote:
    >> How do you tell if a drop down has been placed on a page?
    >>
    >> In this instance I have a form named aForm and I'm checking
    >> for a dropdown named div_list .

    >
    > Use the `type` property:
    > document.aForm.div_list.type
    >
    > It's value is either select-one or select-multiple (the latter for a
    > multiple select object).
    >
    > When you want to know the number of defined options, use:
    > document.aForm.div_list.options.length


    In fact he wanted to know if there is a "select" element in the
    document. Your code can be useful but it will fail if there is
    no "document", "document.aForm" or "document.aForm.div_list".
    So as Lasse wrote, you first need to test if the parents of the
    object and the object itself exist before you can check its "type"
    property. And even then you may trigger a script error as there
    may be no "type" property or reading it is forbidden (e.g. due to
    missing ECMAScript conformance of the UA's script engine). You can
    (and should) test for the former, however you can't test for the
    latter because the test itself triggers the error and you cannot
    catch it. Same goes for options.length.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 4, 2004
    #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. Oodini
    Replies:
    1
    Views:
    1,846
    Keith Thompson
    Sep 27, 2005
  2. Angel Tsankov
    Replies:
    1
    Views:
    899
    Victor Bazarov
    Apr 5, 2006
  3. yogesh
    Replies:
    1
    Views:
    399
    Victor Bazarov
    Mar 14, 2007
  4. Brown, Rodrick

    How can I tell if variable is defined

    Brown, Rodrick, Sep 22, 2009, in forum: Python
    Replies:
    7
    Views:
    464
    Dave Angel
    Sep 23, 2009
  5. Mike A
    Replies:
    17
    Views:
    286
    Dr John Stockton
    Nov 19, 2003
Loading...

Share This Page