Skip to content

Instantly share code, notes, and snippets.

Created May 8, 2014 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f711330df558adc0ce7c to your computer and use it in GitHub Desktop.
Save anonymous/f711330df558adc0ce7c to your computer and use it in GitHub Desktop.
initSelection for preselected select2 value
initSelection: function(element, callback) {
var id=$(element).val();
if (id!=="") {
$.ajax("/countries/"+id+".json", {
dataType: "json"
}).done(function(data) {
var selected = {id: element.val(), text: data.name + " (" + data.country_code + ")"};
callback(selected);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment