Created
December 31, 2019 20:45
-
-
Save DuckDivers/f8a11812ea92361d148238cdf998f92d to your computer and use it in GitHub Desktop.
Choose State / Province from JSON file
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
$('#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