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. Advertisements

  2. 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, Oct 21, 2003
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.