Checking if a select value exists?

K

Keiron Waites

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

Lasse Reichstein Nielsen

Keiron Waites said:
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
 

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

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top