Created
March 28, 2014 19:09
-
-
Save joemeador/9840641 to your computer and use it in GitHub Desktop.
Form autofill city and state fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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