how can make the slect element and append options?
how can make the slect element and append options?
JavaScript
$(document).ready(function () {
var jsonObject = {
"id": "countries",
"label": "Country",
"name": "countries",
"type": "select",
"options": [
{
"value": "",
"text": "Select Country"
},
{
"value": "in",
"text": "India",
"
},
{
"value": "us",
"text": "United Stated"
selected": "true"
},
{
"value": "uk",
"text": "United Kingdom"
},
{
"value": "cn",
"text": "Canada"
}
]
};
var selectedvalue = '';
$("body").append($("<label />", { html: jsonObject.label, 'for': jsonObject.name }),
$("<select id='ddlSelect' />", { id: jsonObject.id, name: jsonObject.name }).html($.map(jsonObject.options, function (entity, i) {
if (entity.selected == "true")
selectedvalue = entity.value;
return '<option value="' + entity.value + '">' + entity.text + '</option>';
//return $("<option />", { value: entity.value, text: entity.text });
}).join('')));
$('#ddlSelect').val(selectedvalue).trigger('change');
});