Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  18] [ 3]  / answers: 1 / hits: 29746  / 13 Years ago, thu, december 8, 2011, 12:00:00

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;

}

More From » javascript

 Answers
85

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.


[#88656] Thursday, December 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;