Skip to content

Instantly share code, notes, and snippets.

@achillar
Created February 11, 2011 19:53
Show Gist options
  • Save achillar/822890 to your computer and use it in GitHub Desktop.
Save achillar/822890 to your computer and use it in GitHub Desktop.
Fill a select with a catalog loaded using JSON and sorts them by name
function sort_cities(a, b) {
var compA = a.name.toUpperCase();
var compB = b.name.toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
}
jQuery( document ).ready( function( $ ) {
$.getJSON('CHANGE_ME_JSON_URL?callback=?', function(data) {
data = jQuery.makeArray(data);
data.sort(sort_cities);
$.each(data, function(i,item){
$('#CITIES_SELECT_ID').append($("<option></option>").attr("value",item.id).html(item.name));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment