Programming Tips - javaScript: How do I append an option to an HTML select

Date: 2009jul6 Update: 2017may16 Language: javaScript Platform: web Q. javaScript: How do I append an option to an HTML <select> A. This function does the trick:
function appendSelectOption(objSelect, pretty, value, is_selected) { if (objSelect == null) return false; let options = objSelect.options; options[options.length] = new Option(pretty, value, false, is_selected); return true; }
An example:
<select id=myselect> </select> <script> let obj = document.getElementById('myselect'); appendSelectOption(obj, 'Yes', 'yes', false); appendSelectOption(obj, 'No', 'no', false); appendSelectOption(obj, 'Maybe', 'maybe', true); </script>
Using this from jQuery:
appendSelectOption($('#myselect')[0], pretty, value, is_selected);