On page load, I\'m trying to use initSelection to select ID 60 (specified value of the input field). I can\'t seem to get it to work properly.
The PHP scrip
Finally solved it! I figured select2
didn't want an array since it's a single value, so I selected the first element of the data.results array.
callback(data.results[0]);
And if you have set multiple:true, just accept the entire results array;
callback(data.results);
You could also use this for less code:
initSelection: function(element, callback) {
return $.getJSON("/jQueryScripts/jQuerySelectListArtists.php?id=" + element.val(), null, function(data) {
return callback(data[0]);
});
}
I saw this example in the Select2 wiki, but i had to deal with callback(data) vs callback(data[0]) like you did.