languagedoor

Start Learning Online with Language Door

[raw]

var populateCities = function (country, target_id) {
// delcare our function variables
var i = 0, options, cities, selection, target;

// our available cities per country, stored as JSON
cities = {
“usa”: {
“dallas”: “dal”,
“san francisco”: “sf”,
“houston”: “hou”
},
“fr”: {
“paris”: “pr”,
“la riche”: “lr”
}
};

// save the selected option that is passed to the function
selection = country.options[country.selectedIndex].value;

// get our target select box by the id that is passed into the function
target = document.getElementById(target_id);

// reset our current target options
target.options.length = 0;

// make sure the selection isn’t none
if(selection !== ‘none’) {
// get our available options
options = cities[selection];

target.options[0] = new Option(‘Pick City’, ‘none’);

i = 1;

// assign the city and value for each option available
for(var city in options) {
target.options[i] = new Option(city, options[city]);
i++;
}
target.style.display = ‘block’;
} else {
target.style.display = ‘none’;
}
}

var linkCity = function (city) {
links = {
“dal” : “https://www.google.com”,
“sf” : “https://www.yahoo.com”
}

// save the selected option that is passed to the function
selection = city.options[city.selectedIndex].value;

// make sure the selection isn’t none
if(selection !== ‘none’) {
link = links[selection];

alert(‘Going to open:’ + link);
window.location.href = link;
}
}
[/raw]