JavaScript: Dynamically creating a drop-down list (select)

From The Travis van der Font Wiki
Jump to: navigation, search
HTML:
<select class="form-control" id="example_select">
   <option value="">--- select an option ---</option>
</select>
Source Code (JavaScript):
var select  = document.getElementById('example_select');
var options = [[111, 'One'], [222, 'Two'], [333, 'Three']];
//var options = document.querySelectorAll('.example_class_name');

options.forEach(function (element)
{
    var option = document.createElement('option');
        option.value = element[0];
        option.text  = element[1];
        
    select.add(option);
});
Output:
<select class="form-control" id="example_select">
   <option value="">--- select an option ---</option>
   <option value="111">One</option>
   <option value="222">Two</option>
   <option value="333">Three</option>
</select>

Additional Information: