Skip to content

Instantly share code, notes, and snippets.

@DuckDivers
Created December 31, 2019 20:45
Show Gist options
  • Save DuckDivers/f8a11812ea92361d148238cdf998f92d to your computer and use it in GitHub Desktop.
Save DuckDivers/f8a11812ea92361d148238cdf998f92d to your computer and use it in GitHub Desktop.
Choose State / Province from JSON file
$('#country').change(function(){
// Clear previous Choices
$('#state').html();
var country = $(this).val();
var json_file;
var states;
if (country === 'US'){
json_file = 'states.json';
states = 'State'
} else if (country === 'CA'){
json_file = 'provinces.json';
states = 'Province';
} else {
return false;
}
var output = '<label for="state">'+states+'<span class="required">*</span></label>';
output += '<select name="state" class="select2" id="state">';
output += '<option value="" selected> - - Choose '+states+' - - </option>';
$('#state_select').html(output);
$.getJSON(duckAjax.theme_uri + '/js/' + json_file, function(data){
$.each( data, function( key, val ) {
$('#state').append("<option value='"+ key +"'>"+ val +"</option>");
});
});
$('#state').after('</select>') ;
}); // End Country Choose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment