This is a working sample, but I would like to use a dynamic variable
"selected_country" within the each loop at the end. Like:
data.selected_country. However, if I use this it takes it literally
and thus does not find "selected_country" in the JSON. It would be
nice to find and only return US, UK, etc. from the JSON.
JS:
var $select_country = $('select[name="country"]').get()[0];
var $select_state = $('select[name="state"]').get()[0];
var selected_country = 'US';
$($select_country).change(function() {
selected_country = $select_country.value;
});
$.getJSON('/portal/js/lib/state.json',function(data) {
$.each(data.US, function(key,state) {
$($select_state).append('<option value="' + key + '">' + state + '</
option>');
});
});
JSON:
{
'US' :
{ 'AK' : 'Alaska', 'AL' : 'Alabama'},
'CA' :
{'AB' : 'Alberta', 'MB' : 'Manitoba'}
}
Any suggestions? Thanks.