By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

Checking if a select value exists?

P: n/a
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.
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Keiron Waites" <webmaster@-NOSPAM-sharemonkey.com> 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[i].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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.