Country, State, City, Javascript dropdown list











up vote
1
down vote

favorite












I'm using the dropdown list contained in http://www.javascriptsource.com/forms/country-state-city-drop-down-list.html



// State lists
var states = new Array();
states['Canada'] = new Array('Alberta', 'British Columbia', 'Ontario');
states['Mexico'] = new Array('Baja California', 'Chihuahua', 'Jalisco');
states['United States'] = new Array('California', 'Florida', 'New York');
// City lists
var cities = new Array();
cities['Canada'] = new Array();
cities['Canada']['Alberta'] = new Array('Edmonton', 'Calgary');
cities['Canada']['British Columbia'] = new Array('Victoria', 'Vancouver');
cities['Canada']['Ontario'] = new Array('Toronto', 'Hamilton');
cities['Mexico'] = new Array();
cities['Mexico']['Baja California'] = new Array('Tijauna', 'Mexicali');
cities['Mexico']['Chihuahua'] = new Array('Ciudad Juárez', 'Chihuahua');
cities['Mexico']['Jalisco'] = new Array('Guadalajara', 'Chapala');
cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles', 'San Francisco');
cities['United States']['Florida'] = new Array('Miami', 'Orlando');
cities['United States']['New York'] = new Array('Buffalo', 'new York');

function setStates() {
cntrySel = document.getElementById('country');
stateList = states[cntrySel.value];
changeSelect('state', stateList, stateList);
setCities();
}

function setCities() {
cntrySel = document.getElementById('country');
stateSel = document.getElementById('state');
cityList = cities[cntrySel.value][stateSel.value];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i = 0; i < newOptions.length; i++) {
selectField.options[selectField.length] = newOption(newOptions[i], newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
setStates();
});
Head < script type = "text/javascript"
src = "countryStateCity.js" > << / script > Body < fieldset style = "width: 230px;" > <legend><strong>Make your selection</strong></legend> < p > <form name="test" method="POST" action="processingpage.php">
<table>
<tr>
<td style="text-align: left;">Country:</td>
<td style="text-align: left;">
<select name="country" id="country" onchange="setStates();">
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United States">United States</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">State:</td>
<td style="text-align: left;">
<select name="state" id="state" onchange="setCities();">
<option value="">Please select a Country</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">City:</td>
<td style="text-align: left;">
<select name="city" id="city">
<option value="">Please select a Country</option>
</select>
</td>
</tr>
</table>
</form> < /fieldset>


I have got the code to work OK but only with small content. When I load all countries, states & regions, & cities & towns into countryStateCity.js file my computer runs out of memory.



The "countryStateCity.js" file is huge. If I list all the countries and all the states & regions and the cities & towns of the countries starting with "A" & "B" everything works OK, but if I add the cities & towns of the countries starting with "C" the system fails.



I need to break up the source file into maybe 1 for each country similar to this;



src="country/Canada.js"
src="country/Mexico.js"
src="country/United States.js"


I want Javascript to find a file name like country/Canada.js, rather than find a name within the whole world file.



Can somebody provide me with amended "Javascript" coding?










share|improve this question
























  • i would load the data from server via ajax.
    – Jonas Wilms
    Feb 19 '17 at 13:26






  • 1




    StackOverlfow is not a free code writing or data structuring service
    – charlietfl
    Feb 19 '17 at 14:18






  • 1




    Developers trust Stack Overflow to help solve coding problems and use Stack Overflow Jobs to find job opportunities. We’re committed to making the internet a better place, and our products aim to enrich the lives of developers as they grow and mature in their careers.
    – user225359
    Feb 21 '17 at 10:05















up vote
1
down vote

favorite












I'm using the dropdown list contained in http://www.javascriptsource.com/forms/country-state-city-drop-down-list.html



// State lists
var states = new Array();
states['Canada'] = new Array('Alberta', 'British Columbia', 'Ontario');
states['Mexico'] = new Array('Baja California', 'Chihuahua', 'Jalisco');
states['United States'] = new Array('California', 'Florida', 'New York');
// City lists
var cities = new Array();
cities['Canada'] = new Array();
cities['Canada']['Alberta'] = new Array('Edmonton', 'Calgary');
cities['Canada']['British Columbia'] = new Array('Victoria', 'Vancouver');
cities['Canada']['Ontario'] = new Array('Toronto', 'Hamilton');
cities['Mexico'] = new Array();
cities['Mexico']['Baja California'] = new Array('Tijauna', 'Mexicali');
cities['Mexico']['Chihuahua'] = new Array('Ciudad Juárez', 'Chihuahua');
cities['Mexico']['Jalisco'] = new Array('Guadalajara', 'Chapala');
cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles', 'San Francisco');
cities['United States']['Florida'] = new Array('Miami', 'Orlando');
cities['United States']['New York'] = new Array('Buffalo', 'new York');

function setStates() {
cntrySel = document.getElementById('country');
stateList = states[cntrySel.value];
changeSelect('state', stateList, stateList);
setCities();
}

function setCities() {
cntrySel = document.getElementById('country');
stateSel = document.getElementById('state');
cityList = cities[cntrySel.value][stateSel.value];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i = 0; i < newOptions.length; i++) {
selectField.options[selectField.length] = newOption(newOptions[i], newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
setStates();
});
Head < script type = "text/javascript"
src = "countryStateCity.js" > << / script > Body < fieldset style = "width: 230px;" > <legend><strong>Make your selection</strong></legend> < p > <form name="test" method="POST" action="processingpage.php">
<table>
<tr>
<td style="text-align: left;">Country:</td>
<td style="text-align: left;">
<select name="country" id="country" onchange="setStates();">
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United States">United States</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">State:</td>
<td style="text-align: left;">
<select name="state" id="state" onchange="setCities();">
<option value="">Please select a Country</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">City:</td>
<td style="text-align: left;">
<select name="city" id="city">
<option value="">Please select a Country</option>
</select>
</td>
</tr>
</table>
</form> < /fieldset>


I have got the code to work OK but only with small content. When I load all countries, states & regions, & cities & towns into countryStateCity.js file my computer runs out of memory.



The "countryStateCity.js" file is huge. If I list all the countries and all the states & regions and the cities & towns of the countries starting with "A" & "B" everything works OK, but if I add the cities & towns of the countries starting with "C" the system fails.



I need to break up the source file into maybe 1 for each country similar to this;



src="country/Canada.js"
src="country/Mexico.js"
src="country/United States.js"


I want Javascript to find a file name like country/Canada.js, rather than find a name within the whole world file.



Can somebody provide me with amended "Javascript" coding?










share|improve this question
























  • i would load the data from server via ajax.
    – Jonas Wilms
    Feb 19 '17 at 13:26






  • 1




    StackOverlfow is not a free code writing or data structuring service
    – charlietfl
    Feb 19 '17 at 14:18






  • 1




    Developers trust Stack Overflow to help solve coding problems and use Stack Overflow Jobs to find job opportunities. We’re committed to making the internet a better place, and our products aim to enrich the lives of developers as they grow and mature in their careers.
    – user225359
    Feb 21 '17 at 10:05













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm using the dropdown list contained in http://www.javascriptsource.com/forms/country-state-city-drop-down-list.html



// State lists
var states = new Array();
states['Canada'] = new Array('Alberta', 'British Columbia', 'Ontario');
states['Mexico'] = new Array('Baja California', 'Chihuahua', 'Jalisco');
states['United States'] = new Array('California', 'Florida', 'New York');
// City lists
var cities = new Array();
cities['Canada'] = new Array();
cities['Canada']['Alberta'] = new Array('Edmonton', 'Calgary');
cities['Canada']['British Columbia'] = new Array('Victoria', 'Vancouver');
cities['Canada']['Ontario'] = new Array('Toronto', 'Hamilton');
cities['Mexico'] = new Array();
cities['Mexico']['Baja California'] = new Array('Tijauna', 'Mexicali');
cities['Mexico']['Chihuahua'] = new Array('Ciudad Juárez', 'Chihuahua');
cities['Mexico']['Jalisco'] = new Array('Guadalajara', 'Chapala');
cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles', 'San Francisco');
cities['United States']['Florida'] = new Array('Miami', 'Orlando');
cities['United States']['New York'] = new Array('Buffalo', 'new York');

function setStates() {
cntrySel = document.getElementById('country');
stateList = states[cntrySel.value];
changeSelect('state', stateList, stateList);
setCities();
}

function setCities() {
cntrySel = document.getElementById('country');
stateSel = document.getElementById('state');
cityList = cities[cntrySel.value][stateSel.value];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i = 0; i < newOptions.length; i++) {
selectField.options[selectField.length] = newOption(newOptions[i], newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
setStates();
});
Head < script type = "text/javascript"
src = "countryStateCity.js" > << / script > Body < fieldset style = "width: 230px;" > <legend><strong>Make your selection</strong></legend> < p > <form name="test" method="POST" action="processingpage.php">
<table>
<tr>
<td style="text-align: left;">Country:</td>
<td style="text-align: left;">
<select name="country" id="country" onchange="setStates();">
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United States">United States</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">State:</td>
<td style="text-align: left;">
<select name="state" id="state" onchange="setCities();">
<option value="">Please select a Country</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">City:</td>
<td style="text-align: left;">
<select name="city" id="city">
<option value="">Please select a Country</option>
</select>
</td>
</tr>
</table>
</form> < /fieldset>


I have got the code to work OK but only with small content. When I load all countries, states & regions, & cities & towns into countryStateCity.js file my computer runs out of memory.



The "countryStateCity.js" file is huge. If I list all the countries and all the states & regions and the cities & towns of the countries starting with "A" & "B" everything works OK, but if I add the cities & towns of the countries starting with "C" the system fails.



I need to break up the source file into maybe 1 for each country similar to this;



src="country/Canada.js"
src="country/Mexico.js"
src="country/United States.js"


I want Javascript to find a file name like country/Canada.js, rather than find a name within the whole world file.



Can somebody provide me with amended "Javascript" coding?










share|improve this question















I'm using the dropdown list contained in http://www.javascriptsource.com/forms/country-state-city-drop-down-list.html



// State lists
var states = new Array();
states['Canada'] = new Array('Alberta', 'British Columbia', 'Ontario');
states['Mexico'] = new Array('Baja California', 'Chihuahua', 'Jalisco');
states['United States'] = new Array('California', 'Florida', 'New York');
// City lists
var cities = new Array();
cities['Canada'] = new Array();
cities['Canada']['Alberta'] = new Array('Edmonton', 'Calgary');
cities['Canada']['British Columbia'] = new Array('Victoria', 'Vancouver');
cities['Canada']['Ontario'] = new Array('Toronto', 'Hamilton');
cities['Mexico'] = new Array();
cities['Mexico']['Baja California'] = new Array('Tijauna', 'Mexicali');
cities['Mexico']['Chihuahua'] = new Array('Ciudad Juárez', 'Chihuahua');
cities['Mexico']['Jalisco'] = new Array('Guadalajara', 'Chapala');
cities['United States'] = new Array();
cities['United States']['California'] = new Array('Los Angeles', 'San Francisco');
cities['United States']['Florida'] = new Array('Miami', 'Orlando');
cities['United States']['New York'] = new Array('Buffalo', 'new York');

function setStates() {
cntrySel = document.getElementById('country');
stateList = states[cntrySel.value];
changeSelect('state', stateList, stateList);
setCities();
}

function setCities() {
cntrySel = document.getElementById('country');
stateSel = document.getElementById('state');
cityList = cities[cntrySel.value][stateSel.value];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i = 0; i < newOptions.length; i++) {
selectField.options[selectField.length] = newOption(newOptions[i], newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
setStates();
});
Head < script type = "text/javascript"
src = "countryStateCity.js" > << / script > Body < fieldset style = "width: 230px;" > <legend><strong>Make your selection</strong></legend> < p > <form name="test" method="POST" action="processingpage.php">
<table>
<tr>
<td style="text-align: left;">Country:</td>
<td style="text-align: left;">
<select name="country" id="country" onchange="setStates();">
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United States">United States</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">State:</td>
<td style="text-align: left;">
<select name="state" id="state" onchange="setCities();">
<option value="">Please select a Country</option>
</select>
</td>
</tr><tr>
<td style="text-align: left;">City:</td>
<td style="text-align: left;">
<select name="city" id="city">
<option value="">Please select a Country</option>
</select>
</td>
</tr>
</table>
</form> < /fieldset>


I have got the code to work OK but only with small content. When I load all countries, states & regions, & cities & towns into countryStateCity.js file my computer runs out of memory.



The "countryStateCity.js" file is huge. If I list all the countries and all the states & regions and the cities & towns of the countries starting with "A" & "B" everything works OK, but if I add the cities & towns of the countries starting with "C" the system fails.



I need to break up the source file into maybe 1 for each country similar to this;



src="country/Canada.js"
src="country/Mexico.js"
src="country/United States.js"


I want Javascript to find a file name like country/Canada.js, rather than find a name within the whole world file.



Can somebody provide me with amended "Javascript" coding?







javascript html drop-down-menu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 19 '17 at 15:17









m87

3,80021030




3,80021030










asked Feb 19 '17 at 13:23









user225359

14117




14117












  • i would load the data from server via ajax.
    – Jonas Wilms
    Feb 19 '17 at 13:26






  • 1




    StackOverlfow is not a free code writing or data structuring service
    – charlietfl
    Feb 19 '17 at 14:18






  • 1




    Developers trust Stack Overflow to help solve coding problems and use Stack Overflow Jobs to find job opportunities. We’re committed to making the internet a better place, and our products aim to enrich the lives of developers as they grow and mature in their careers.
    – user225359
    Feb 21 '17 at 10:05


















  • i would load the data from server via ajax.
    – Jonas Wilms
    Feb 19 '17 at 13:26






  • 1




    StackOverlfow is not a free code writing or data structuring service
    – charlietfl
    Feb 19 '17 at 14:18






  • 1




    Developers trust Stack Overflow to help solve coding problems and use Stack Overflow Jobs to find job opportunities. We’re committed to making the internet a better place, and our products aim to enrich the lives of developers as they grow and mature in their careers.
    – user225359
    Feb 21 '17 at 10:05
















i would load the data from server via ajax.
– Jonas Wilms
Feb 19 '17 at 13:26




i would load the data from server via ajax.
– Jonas Wilms
Feb 19 '17 at 13:26




1




1




StackOverlfow is not a free code writing or data structuring service
– charlietfl
Feb 19 '17 at 14:18




StackOverlfow is not a free code writing or data structuring service
– charlietfl
Feb 19 '17 at 14:18




1




1




Developers trust Stack Overflow to help solve coding problems and use Stack Overflow Jobs to find job opportunities. We’re committed to making the internet a better place, and our products aim to enrich the lives of developers as they grow and mature in their careers.
– user225359
Feb 21 '17 at 10:05




Developers trust Stack Overflow to help solve coding problems and use Stack Overflow Jobs to find job opportunities. We’re committed to making the internet a better place, and our products aim to enrich the lives of developers as they grow and mature in their careers.
– user225359
Feb 21 '17 at 10:05












1 Answer
1






active

oldest

votes

















up vote
0
down vote













OK. This is simple to do. I don't have access to your country file so I don't know how it is scoped or broken up, IE arrays or JSON. So my code is just a mash of yours and it will not work. But it shows how you can solve your problem with a fast end result for the user experience.



The trick is just to load the data that is needed when you need it. Usually this is done with an API, you call it via ajax tell it what you want and boom it sends it back so your UI can show it.



In this scenario we don't have a server so we will do it by dynamically loading scripts into the dom. In this case country data filled with states and City's. One file per country when requested.



It would be better to load state data then cities per state and so on. As well as potentially considering if data should be removed from the Dom if memory is an issue:






//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});









share|improve this answer























  • The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
    – MartinWebb
    Feb 19 '17 at 14:21










  • Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
    – user225359
    Feb 21 '17 at 10:08










  • Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
    – MartinWebb
    Feb 21 '17 at 12:06













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42327733%2fcountry-state-city-javascript-dropdown-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













OK. This is simple to do. I don't have access to your country file so I don't know how it is scoped or broken up, IE arrays or JSON. So my code is just a mash of yours and it will not work. But it shows how you can solve your problem with a fast end result for the user experience.



The trick is just to load the data that is needed when you need it. Usually this is done with an API, you call it via ajax tell it what you want and boom it sends it back so your UI can show it.



In this scenario we don't have a server so we will do it by dynamically loading scripts into the dom. In this case country data filled with states and City's. One file per country when requested.



It would be better to load state data then cities per state and so on. As well as potentially considering if data should be removed from the Dom if memory is an issue:






//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});









share|improve this answer























  • The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
    – MartinWebb
    Feb 19 '17 at 14:21










  • Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
    – user225359
    Feb 21 '17 at 10:08










  • Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
    – MartinWebb
    Feb 21 '17 at 12:06

















up vote
0
down vote













OK. This is simple to do. I don't have access to your country file so I don't know how it is scoped or broken up, IE arrays or JSON. So my code is just a mash of yours and it will not work. But it shows how you can solve your problem with a fast end result for the user experience.



The trick is just to load the data that is needed when you need it. Usually this is done with an API, you call it via ajax tell it what you want and boom it sends it back so your UI can show it.



In this scenario we don't have a server so we will do it by dynamically loading scripts into the dom. In this case country data filled with states and City's. One file per country when requested.



It would be better to load state data then cities per state and so on. As well as potentially considering if data should be removed from the Dom if memory is an issue:






//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});









share|improve this answer























  • The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
    – MartinWebb
    Feb 19 '17 at 14:21










  • Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
    – user225359
    Feb 21 '17 at 10:08










  • Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
    – MartinWebb
    Feb 21 '17 at 12:06















up vote
0
down vote










up vote
0
down vote









OK. This is simple to do. I don't have access to your country file so I don't know how it is scoped or broken up, IE arrays or JSON. So my code is just a mash of yours and it will not work. But it shows how you can solve your problem with a fast end result for the user experience.



The trick is just to load the data that is needed when you need it. Usually this is done with an API, you call it via ajax tell it what you want and boom it sends it back so your UI can show it.



In this scenario we don't have a server so we will do it by dynamically loading scripts into the dom. In this case country data filled with states and City's. One file per country when requested.



It would be better to load state data then cities per state and so on. As well as potentially considering if data should be removed from the Dom if memory is an issue:






//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});









share|improve this answer














OK. This is simple to do. I don't have access to your country file so I don't know how it is scoped or broken up, IE arrays or JSON. So my code is just a mash of yours and it will not work. But it shows how you can solve your problem with a fast end result for the user experience.



The trick is just to load the data that is needed when you need it. Usually this is done with an API, you call it via ajax tell it what you want and boom it sends it back so your UI can show it.



In this scenario we don't have a server so we will do it by dynamically loading scripts into the dom. In this case country data filled with states and City's. One file per country when requested.



It would be better to load state data then cities per state and so on. As well as potentially considering if data should be removed from the Dom if memory is an issue:






//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});









//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});






//
// Some globals to store what the user selects.
//
VAR _COUNTY, _STATE, _CITY;

//
// This will append scripts to the body so that you dont need to load all the
// scripts in one go, this is the trick.
//
function addScript( src,callback) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.onload=callback;
document.body.appendChild( s );
}
//
// Get all the states based on country
// add the correct file to the dom
// ie: country/Canada.js
// these files would be better scoped geodata.country geodata.state.
//
// IMPORTANT CHANGE YOUR HTML SELECT HANDLER TO FIRE HERE.
// <select name="country" id="country" onchange="getStates();">
//
function getStates(){
_COUNRTY = document.getElementById('country'),
file = ["country/",_COUNTRY,".js"].join;
addScript(file, setStates);
};
//
//
// Called when the script loads
//
function setStates() {
//
// Here is where we would do
// geodata.states for now I'm using your declaration of state.
//
var stateList = states;
changeSelect('state', stateList, stateList);
// Not sure why you are doing this they need to select a state first?
setCities();
}
//
// Called when they select a state,
// I would replicate the state code and load all the cities per state.
// I would disable the city drop down until the states are loaded.
//
function setCities() {
_STATE = document.getElementById('state');
cityList = cities[_COUNTRY][_STATE];
changeSelect('city', cityList, cityList);
}

function changeSelect(fieldID, newOptions, newValues) {
selectField = document.getElementById(fieldID);
selectField.options.length = 0;
for (i=0; i<newOptions.length; i++) {
selectField.options[selectField.length]=newOption(newOptions[i],newValues[i]);
}
}

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(function() {
setStates();
});







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 19 '17 at 14:23

























answered Feb 19 '17 at 14:09









MartinWebb

1,3831611




1,3831611












  • The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
    – MartinWebb
    Feb 19 '17 at 14:21










  • Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
    – user225359
    Feb 21 '17 at 10:08










  • Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
    – MartinWebb
    Feb 21 '17 at 12:06




















  • The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
    – MartinWebb
    Feb 19 '17 at 14:21










  • Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
    – user225359
    Feb 21 '17 at 10:08










  • Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
    – MartinWebb
    Feb 21 '17 at 12:06


















The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
– MartinWebb
Feb 19 '17 at 14:21




The code that was provided in this question is not elaborate enough to solve the problem. My answer is a hack of the code to show how using dynamic script loading can solve the problem. However my answer leaves a lot in question, since we don't have access to the data file nor how it is scoped and comprised. And a lot of code would need to be written to make this a reliable solution. And as others have state this is not a free code writing service.
– MartinWebb
Feb 19 '17 at 14:21












Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
– user225359
Feb 21 '17 at 10:08




Thanks Martin, I have found the problem. The problem was in the country file. There were some characters there that shouldn't have been there and it threw the whole system out. And they took a lot of time to find them.
– user225359
Feb 21 '17 at 10:08












Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
– MartinWebb
Feb 21 '17 at 12:06






Do i win the answer ? My solution answers the question of how to load the data on the fly as the user makes his selection? That was the question and I think my solution solves that problem all be-it not a working solution as I did not have the data.
– MartinWebb
Feb 21 '17 at 12:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42327733%2fcountry-state-city-javascript-dropdown-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen