Skip to content

Instantly share code, notes, and snippets.

@joemeador
Created March 28, 2014 19:09
Show Gist options
  • Save joemeador/9840641 to your computer and use it in GitHub Desktop.
Save joemeador/9840641 to your computer and use it in GitHub Desktop.
Form autofill city and state fields
jQuery('#field_65185').keyup(function () { //the meat of this was inspired by css-tricks.com
var el = jQuery(this);
if (jQuery('#field_65185').val().length === 5) {
jQuery.ajax({
url: 'http://zip.elevenbasetwo.com/v2/US/' + el.val(),
cache: false,
dataType: 'json',
type: 'GET',
data: 'field_65185=' + el.val(),
success: function (result, success) {
jQuery('#field_65182').val(result.city); /* Fill the data */
jQuery('#field_65186').val(result.state);
jQuery('.cat-request .info_block.city').slideDown(); /* Show the fields */
jQuery('.cat-request .info_block.state').slideDown(); /* Show the fields */
jQuery('.field_error').slideUp(300);
},
error: function (result, success) {
if (jQuery('#bronto select option:selected:contains("United States")')) {
jQuery('.field_error').slideDown(300);
}
}
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment