Forum Moderators: open
if(document.myForm.mySelectBox.selectedIndex == 1){
then do this
}
I wonder if there is a way to check if the selected index is for example 1, 3 or 4 without having to write the above line 3 times.
Something like this is what I want but this solution does not work for me.
if(document.myForm.mySelectBox.selectedIndex == 1 ¦¦ 3 ¦¦ 4){
then do this
}
var i = document.myForm.mySelectBox.selectedIndex;
if ((i == 1) ¦¦ (i == 3) ¦¦ (i == 4)) {
// then do this
}
Or may be (just a thought)...
var valid_values = '.x.xx'; // NB: Char positions 1, 3 and 4
if (valid_values.charAt(document.myForm.mySelectBox.selectedIndex) == 'x') {
// then do this
}
Or, similar to above, add an
in_arraymethod to the Array object (doesn't exist in JS by default) and then use an Array instead of the string above to hold your valid values. ie.
var valid_values = [1,3,4];(which is easier to read and debug)
Or...