JSFiddle - React, Tailwind, and code Playground
HTML
<select id="meSelect">
</select>
<select id="meSelect2">
</select>
JavaScript
for(var i=0;i<5;i++){
appendOptions("meSelect", "value"+i, "text"+i);
appendOptions2("meSelect2", "valuea"+i, "texta"+i);
}
$("#meSelect").val("value2");
$("#meSelect2").val("valuea2");
//alert($("#mySelect option").length);
//alert($("#meSelect2 option").length);
function appendOptions(selId, val, txt)
{
var sel = $("#"+selId)[0], options=sel.options;
options[options.length] = new Option(txt,val);
}
function appendOptions2(selId, val, txt)
{
var opt = document.createElement('option');
opt.text = txt;
opt.value = val;
var sel = document.getElementById(selId);
try {
sel.add(opt, null); // standards compliant; doesn't work in IE
}catch(ex){
sel.add(opt); // IE only
}
}