View Single Post
Old 10-09-2012, 08:49 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Is this what you are wanting?

Code:
<html>
<head>

<script type="text/javascript">

var countries = [ ];
countries["Egypt"]   = ["Cairo","Alexandria","Sharm","Luxor"];
countries["Germany"] = ["Munchen","Dortmund","Berlin","Koln", "Nurnberg"];
countries["France"] = ["Paris", "Rouen", "Lille", "Lyon", "Marseilles"];
countries["USA"]     = ["New York","Chicago","Dallas","Seattle","Los Angeles", "Atlanta", "Boston", "Miami"];
countries["Japan"]   = ["Tokyo","Osaka","Kyoto","Sapporo"];

function switchCountry(selCountry) {
var citySel = selCountry.form.City;
for ( var s = citySel.options.length-1; s > 0; --s )  {
citySel.options[s] = null;
}

var chosen = selCountry.options[selCountry.selectedIndex].text;
var cList = countries[chosen];
if ( cList != null )   {
for ( var i = 0; i < cList.length; ++i )   {
citySel.options[i+1] = new Option(cList[i],cList[i]);
}
}

}

function showSelection() {
var country = document.forms[0].Country.value;
var city = document.forms[0].City.value;
if (city != "") {
alert ("You selected " + city + " in " + country);
}
}

</script>

</head>
<body>
<form>
<select name="Country" onchange="switchCountry(this);">
<option value = "">Choose Country</option>
<option value = "Egypt">Egypt</option>
<option value = "Germany">Germany</option>
<option value = "France">France</option>
<option value = "Japan">Japan</option>
<option value = "USA">USA</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<select name="City" onchange = "showSelection()">
<option>Choose City</option>
</select>
</form>
</body>
</html>

Quizmaster: Which Russian leader is credited as the architect of glasnost?
Contestant: Stalin
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
msurya (10-09-2012)