I am adding options to the html select dynamically using jQuery at the same time I am setting the default value, but the default value is not shown.
Here is the bare html code initially.
<div class="form-group">
<select id="f_id" class="form-control">
</select>
</div>
and jQuery code where I am adding the options dynamically
var $fTypeSelect = $('#f_id');
$.each(fVal, function(idx, obj){
$fTypeSelect
.append($("<option></option>").attr("value",obj["ftype_id"]).text(obj["fType"]));
});
So far it has added the options to drop down and it works.
I tried to set the default value in the following ways, none of them is showing as selected.
$fTypeSelect.val(fTypeDefaultVal);//Not shown as the default value
$fTypeSelect.val(fTypeDefaultVal).change();// Not shown as default value
$fTypeSelect.trigger("chosen:updated");//Default value is still not shown
My drop down in the UI is as shown below.
When drop down is expanded all the options are found. How to set the default value? My jQuery version is 2.1.4.