I am calling an URL using Ajax to receive data and populate it inside a select element which is using Select2 and remote data.
The remote data is loading absolutely fine when searching inside the select, but with the below code it doesn’t seem to be creating/selecting the option inside the for loop.
The console is showing the correct value returned from the Ajax call but it just doesn’t select it in the select element
$.ajax({
type: "GET",
url: "/section/tickets?action=get_update_details&seq=" + $(el).attr("id"),
cache: false,
dataType: "json",
success: function(data) {
if(data.emails.to !== "") {
for(var i in data.emails.to) {
console.log(data.emails.to[i]);
var x = new Option(data.emails.to[i], data.emails.to[i], true, true);
$("#contacts_to").append(x).trigger('change');
}
}
}
});
2
Answers
I tried to simulate what you are trying to achieve, can u point out the troubled part.
It is hard to debug without a complete example. However, your code seems to be working fine.
See this modified version: