The following function gets json data from URL and populates a select element with jQuery. I am than using Select2 to transform that drop down in a field with autocomplete function.
Everything works fine apart from the writing 'undefined' that I get as soon as the select elements get displayed. The autocomplete and drop down work perfectly. I have tried to use the data placeholder even by adding an empty 'option' element but no success.
function CitiesList(callback){
$.getJSON(document.URL+'getCities/sdfsfs', function(data){
var html = '';
var len = data.length;
var option = '<option></option>';
for (var i = 0; i< len; i++) {
html += '<option value="' + data[i] + '">' + data[i] + '</option>';
}
$('.select_cities select').append(option);
$('.select_cities select').append(html);
if(callback && typeof callback == 'function'){
callback.call(null);
}
});
}
<select data-placeholder="Select a city" name="cities" id="cities">
</select>
'select_cities' is the div wrapper around the select element.