1/* assuming we have the following HTML
2<select id="s">
3 <option>First</option>
4 <option>Second</option>
5 <option>Third</option>
6</select>
7*/
8
9var s = document.getElementById('s');
10var options = [ 'zero', 'one', 'two' ];
11
12options.forEach(function(element, key) {
13 if (element == 'zero') {
14 s[s.options.length] = new Option(element, s.options.length, false, false);
15 }
16 if (element == 'one') {
17 s[s.options.length] = new Option(element, s.options.length, true, false); // Will add the "selected" attribute
18 }
19 if (element == 'two') {
20 s[s.options.length] = new Option(element, s.options.length, false, true); // Just will be selected in "view"
21 }
22});
23
24/* Result
25<select id="s">
26 <option value="0">zero</option>
27 <option value="1" selected="">one</option>
28 <option value="2">two</option> // User will see this as 'selected'
29</select>
30*/