I want to do something similar to this but I am unable to change the selectedIndex value this way:
var selected = document.getElementById("state-select");
switch (state) {
case 'VA':
selected.options[selected.selectedIndex] = 0;
break;
case 'NC':
selected.options[selected.selectedIndex] = 1;
break;
case 'SC':
selected.options[selected.selectedIndex] = 2;
break;
}
For this purpose you don't need to be doing anything with options, you can change the selected element by setting the .selectedIndex property of your select element directly:
...
case 'VA':
selected.selectedIndex = 0;
break;
// etc.
(Assuming this is a single-select select element.)
I believe if you set selectedIndex to -1 it will leave no options selected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With