Click to See Complete Forum and Search --> : Odd Dropdown list behavior


bsd12673
12-11-2003, 08:40 AM
I have the following code to populate a dropdown list of Countries based upon what state is selected in my states dropdown.
if I select a US based state from my states dropdown list it creates the options in the Countries dropdown to only show U.S. and Canada.

If someone then selects OutsideUS the second dropdown will show a number of countries excluding U.S. and Canada.

The weird thing is if someone selects OutsideUS then decides to select a U.S. or Canadian state it prepends U.S. and Canada to all the other countries. But if I go back and select OutsideUs it shows all the countries but U.S. and Canada. I'm not sure if the way I wrote this function is entirely correct, it works for the most part except for the problem I am having. The code is below.

Thanks,
Ben

<code>
function checkState() {
usCountries = new Array("United States", "Canada");
nonUsCountries = new Array(
"Argentina",
"Australia",
"Austria",
"Belgium",
"Bulgaria",
"Czech Republic",
"Denmark",
"Egypt",
"Finland",
"France",
"Germany",
"Greece",
"Greenland",
"Hong Kong",
"Hungary",
"Iceland",
"India",
"Israel",
"Italy",
"Japan",
"Kuwait",
"Luxembourg",
"Mexico",
"Netherlands",
"New Zealand",
"Norway",
"Poland",
"Portugal",
"Saudi Arabia",
"Singapore",
"Slovakia",
"South Africa",
"Spain",
"Sweden",
"Switzerland",
"Taiwan",
"Turkey",
"United Arab Emirates",
"United Kingdom",
"Yugoslavia",
"Ireland",
"China",
"Albania",
"Belarus",
"Bosnia",
"Herzegovina",
"Estonia",
"Latvia",
"Lithuania",
"Slovenia",
"Ukraine",
"Brazil",
"Bolivia",
"Chile",
"Columbia",
"Paraguay",
"Peru",
"Venezuela",
"Malaysia",
"Thailand",
"Philippines",
"Algeria",
"American Samoa",
"Andorra",
"Angola",
"Anguilla",
"Antigua And Barbuda",
"Armenia",
"Aruba",
"Azerbaijan",
"Bahamas",
"Bahrain",
"Bangladesh",
"Barbados",
"Belize",
"Benin",
"Bermuda",
"Bhutan",
"Botswana",
"Brunei Darussalam",
"Burkina Faso",
"Burma (Myanmar)",
"Burundi",
"Cambodia",
"Cameroon",
"Cape Verde",
"Cayman Islands",
"Central African Republic",
"Chad",
"Christmas Island",
"Cocos (Keeling) Islands",
"Comoros",
"Congo",
"Cook Islands",
"Costa Rica",
"Croatia (Local Name: Hrvatska)",
"Cyprus",
"Djibouti",
"Dominica",
"Dominican Republic",
"East Timor",
"Ecuador",
"El Salvador",
"Equatorial Guinea",
"Eritrea",
"Ethiopia",
"Falkland Islands (Malvinas)",
"Faroe Islands",
"Fiji",
"French Guiana",
"French Polynesia",
"Gabon",
"Gambia",
"Georgia",
"Ghana",
"Gibraltar",
"Grenada",
"Guadeloupe",
"Guam",
"Guatemala",
"Guinea",
"Guinea-Bissau",
"Guyana",
"Haiti",
"Heard And Mc Donald Islands",
"Holy See,Vatican City State",
"Honduras",
"Indonesia",
"Ivory Coast (Cote D'Ivoire)",
"Jamaica",
"Jordan",
"Kazakhstan",
"Kenya",
"Kiribati",
"Korea Republic Of",
"Kyrgyzstan",
"Laos",
"Lebanon",
"Lesotho",
"Liberia",
"Liechtenstein",
"Macau",
"Macedonia (Republic Of)",
"Madagascar",
"Malawi",
"Maldives",
"Mali",
"Marshall Islands",
"Martinique",
"Mauritania",
"Mauritius",
"Micronesia Federated States Of",
"Moldova Republic Of",
"Monaco",
"Mongolia",
"Montserrat",
"Mozambique",
"Namibia",
"Nauru",
"Nepal",
"Netherlands Antilles",
"New Caledonia",
"Nicaragua",
"Niger",
"Nigeria",
"Niue",
"Norfolk Island",
"Northern Mariana Islands",
"Oman",
"Pakistan",
"Palau",
"Panama",
"Papua New Guinea",
"Pitcairn",
"Puerto Rico",
"Qatar",
"Reunion",
"Romania",
"Rwanda",
"Saint Helena",
"Saint Kitts And Nevis",
"Saint Lucia",
"Saint Pierre And Miquelon",
"Saint Vincent & The Grenadines",
"Samoa",
"San Marino",
"Sao Tome And Principe",
"Senegal",
"Seychelles",
"Sierra Leone",
"Solomon Islands",
"Somalia",
"South Georgia & S.Sandwich Isld",
"Sri Lanka",
"Suriname",
"Svalbard & Jan Mayen",
"Swaziland",
"Tajikistan",
"Tanzania",
"Togo",
"Tokelau",
"Tonga",
"Trinidad & Tobago",
"Tunisia",
"Turkmenistan",
"Turks & Caicos Islands",
"Tuvalu",
"Uruguay",
"Uzbekistan",
"Vanuatu",
"Vietnam",
"Virgin Islands (British)",
"Virgin Islands (U.S.)",
"Western Sahara",
"Yemen",
"Zambia",
"Zimbabwe",
"Russia",
"Serbia & Montenegro");

if (document.checkform.request_state.value == "OutsideUS"){
for (i=0; i<nonUsCountries.length; i++) {
document.checkform.request_country.options[i] = new Option(nonUsCountries[i]);
document.checkform.request_country.options[i].value = nonUsCountries[i];
}
}
else {

for (j=0; j<usCountries.length; j++) {
document.checkform.request_country.options[j] = new Option(usCountries[j]);
document.checkform.request_country.options[j].value = usCountries[j];
}
}
}
</code>

MichaelM
12-11-2003, 10:28 AM
You need to clear the existing <option>s from the select box...

[previous code...]
"Serbia & Montenegro");

document.checkform.request_country.options.length = 0;

if (document.checkform.request_state.value == "OutsideUS") {
[remaining code...]

bsd12673
12-11-2003, 10:58 AM
Excellent! Thanks.

Ben