javascript option

Solutions on MaxInterview for javascript option by the best coders in the world

showing results for - "javascript option"
Hans
26 Aug 2020
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*/
queries leading to this page
option javascripthow to assign an option value to a dom object in javascripotoption js 21 options jssetting up html 3cselect 3e options with apijavascript with html optionoption object content javascriptjquery new optiothis option javascriptnew option with html jsoption function jsw3schools js optionsoption in jsjavascript select optionoptions in options javascriptoption html element jsoptions in class javascriptadd critic option in jscss dom optionjavascript new optionnew option select javascriptoptions in javascriptoption chaning jsjs option 2b 3delement option javascriptselect in javascript optionsetting options javascriptjuqery new optionoption select javascriptset new option within function javascriptjs with optionshow to make option tags with selected of select box in javascript 3c 2foption 3e in jsoption constructor javascriptoption function in javascriptoptions html javascriptjavascript select option elementfor in option html js options javascriptjs optionoption javascriptelement option javascriptjs select optionoptions javascriptdom optionsjquery new optionjavascript option functionjavascript option elementoption class in javascriptjavascript option selectjavascript new select optionnew htmloptionelement javascriptjs option 3d 2bhow to use option in jsjavascript optionjavascript option htmloption html javascriptoption html jsnew option javascriptjavascript option boxoption in javascriptnew option jsjavascript with as optionjs option 5b 27usp 27 5doption select jsoption jshtml option jsoption box jsoption on select javascriptjs option constructornew optionjs new optionhtml option domjavascritp optionjavascript option objectoption select html jscreate element in javascript optionjavascript with as optionselect option js 3coption 3e jsoption class javascriptjavascript option