Checking if a select value exists?

Discussion in 'Javascript' started by Keiron Waites, Oct 21, 2003.

  1. I'd like to do something like this, where the script checks to see if an
    option exists:

    if (document.formname.selectname.options["optionvalue"]) != NULL) {
    do something;
    }

    So it would return true if <option value="optionvalue"></option> existed.
    Any ideas?

    Thanks.
    Keiron Waites, Oct 21, 2003
    #1
    1. Advertising

  2. "Keiron Waites" <> writes:

    > I'd like to do something like this, where the script checks to see if an
    > option exists:
    >
    > if (document.formname.selectname.options["optionvalue"]) != NULL) {
    > do something;
    > }


    You would have to iterate through the options.

    function optionValueIndexOf(options,value) {
    for (var i=0;i<options.length;i++) {
    if (options.value == value) {
    return i;
    }
    }
    return -1;
    }

    then you can write:
    if (
    optionValueIndexOf(document.forms['formname'].elements['selectname'].options,
    "optionvalue") >= 0
    ) {
    // do something;
    }

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 21, 2003
    #2
    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. Darrel
    Replies:
    4
    Views:
    3,600
    darrel
    Dec 10, 2004
  2. Totan
    Replies:
    0
    Views:
    923
    Totan
    Apr 17, 2006
  3. Ulf Meinhardt
    Replies:
    8
    Views:
    6,118
  4. palmiere
    Replies:
    1
    Views:
    388
    Erwin Moller
    Feb 9, 2004
  5. Replies:
    2
    Views:
    126
    John Bokma
    Nov 19, 2005
Loading...

Share This Page