Ok so I have a page with several select2 dropdowns that each use a remote source JSON API endpoint (which I also built) for their content. We have a second API endpoint that has some suggested/likely options which we’d like to load into each dropdown on every page load, since it’s likely (almost guaranteed) that they’ll be used over the full search first option.
The only information I can find is how to load a single default using the “new Option” method but no matter what I do, I can’t figure out how to get more than the first option loaded.
The closest I’ve come is with this:
var thing = $('.thing');
var preload = $.ajax({
type: 'GET',
url: '/preload.json',
dataType: 'json',
success: function(data) {
$.each(data, function(key, value){
var option = new Option(value.text, value.id, false, false)
thing.append(option).trigger('change');
thing.trigger({
type: 'select2:select',
data: value
})
})
}
});
Does anyone have any suggestions on how to do what I’m looking for, or am I hooped?