Skip to content

Instantly share code, notes, and snippets.

@DavidGoussev
Created June 14, 2016 15:10
Show Gist options
  • Select an option

  • Save DavidGoussev/9ef3fcce62d43d4f6f3ed720ec502a64 to your computer and use it in GitHub Desktop.

Select an option

Save DavidGoussev/9ef3fcce62d43d4f6f3ed720ec502a64 to your computer and use it in GitHub Desktop.
Hackathon get API data
function ($) {
$('button').on('click', function () {
// remove resultset if this has already been run
$('.content ul').remove();
// add spinner to indicate something is happening
$('<i class="fa fa-refresh fa-spin"/>').appendTo('body');
// get selected zip code from selectbox
var zip = $('select option:selected').text().substring(1, 6);
// make the AJAX request
$.getJSON('http://data.colorado.gov/resource/4ykn-tg5h.json?entityStatus=Good%20Standing&principalZipCode=' + zip, function (data) {
// do all this on success
var items = [],
$ul;
$.each(data, function (key, val) {
//iterate through the returned data and build a list
items.push('<li id="' + key + '"><span class="name">' + val.entityname + '</span><br><span class="addr">' + val.principaladdress1 + '</span> <span class="city">' + val.principalcity + '</span></li>');
});
// if no items were returned then add a message to that effect
if (items.length < 1) {
items.push('<li>No results for this ZIP code, try again!</li>');
}
// remove spinner
$('.fa-spin').remove();
// append list to page
$ul = $('<ul />').appendTo('.content');
//append list items to list
$ul.append(items);
});
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment