Checking if a select value exists?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keiron Waites

    Checking if a select value exists?

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

    if (document.formn ame.selectname. options["optionvalu e"]) != NULL) {
    do something;
    }

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

    Thanks.


  • Lasse Reichstein Nielsen

    #2
    Re: Checking if a select value exists?

    "Keiron Waites" <webmaster@-NOSPAM-sharemonkey.com > writes:
    [color=blue]
    > I'd like to do something like this, where the script checks to see if an
    > option exists:
    >
    > if (document.formn ame.selectname. options["optionvalu e"]) != NULL) {
    > do something;
    > }[/color]

    You would have to iterate through the options.

    function optionValueInde xOf(options,val ue) {
    for (var i=0;i<options.l ength;i++) {
    if (options[i].value == value) {
    return i;
    }
    }
    return -1;
    }

    then you can write:
    if (
    optionValueInde xOf(document.fo rms['formname'].elements['selectname'].options,
    "optionvalu e") >= 0
    ) {
    // do something;
    }

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...