jQuery-- Populate select from json
I have a map in my java sevlet and converting it to a json format that works right.
When I do this function below it creates a drop down, but it puts every character as an option?? This is what I got:
$(document).ready(function(){
var temp= '${temp}';
//alert(options);
var $select = $('#down');
$select.find('option').remove();
$.each(temp, function(key, value) {
$('<option>').val(key).text(value).appendTo($select);
});
});
map content in JSON format
{"1" : "string","2" : "string"}
I would do something like this:
$.each(temp, function(key, value) {
$select.append(`<option value="${key}">${value}</option>`);
});
JSON structure would be appreciated. At first you can experiment with find('element')
- it depends on JSON.
Only change the DOM once...
var listitems = '';
$.each(temp, function(key, value){
listitems += '<option value=' + key + '>' + value + '</option>';
});
$select.append(listitems);
fiddle
var $select = $('#down');
$select.find('option').remove();
$.each(temp,function(key, value)
{
$select.append('<option value=' + key + '>' + value + '</option>');
});