M
MG
I am learning javascript from a book.
The example below is aimed at demonstrating drop down select menus. The
alerts (commented out) were added by me.
The example uses arrays within arrays, but doesn't explain them at all well.
So I have some questions:
What syntax would return Australia, France, Japan, New Zealand ?
Why does cities[1][1] not work, but cities['France'][1] does?
Any other useful help with arrays within arrays would be appreciated.
Thanks
MG
=====================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Related Select Test</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
<!--
// Create an array to hold the cities for each country
var cities = new Array(4);
cities["Australia"] =
["Sydney", "Melbourne", "Canberra", "Perth", "Brisbane"];
cities["France"] =
["Paris", "Lyons", "Nice", "Dijon"];
cities["Japan"] = ["Tokyo", "Kyoto", "Osaka", "Nara"];
cities["New Zealand"] =
["Auckland", "Wellington", "Christchurch", "Dunedin", "Queenstown"];
//alert(cities); does not work
//alert(cities[1]); undefined
//alert(cities[1][1]); does not work
//alert(cities['France']); works
//alert(cities['France'][1]); works
function removeOptions(optionMenu)
{
for (var i=0; i < optionMenu.options.length; i++)
optionMenu.options = null;
}
function addOptions(optionList, optionMenu)
{
removeOptions(optionMenu); // clear out the options
for (var i=0; i < optionList.length; i++)
optionMenu = new Option(optionList, optionList);
}
//-->
</script>
</head>
<body>
<h2>Vacation Chooser</h2>
<form name="testform" id="testform" action="#" method="get">
Country:
<select name="country" id="country"
onchange="addOptions(cities[this.options[this.selectedIndex].text],document.testform.city);">
<option selected="selected">Australia</option>
<option>France</option>
<option>Japan</option>
<option>New Zealand</option>
</select>
City:
<select name="city" id="city">
<option>Sydney</option>
<option>Melbourne</option>
<option>Canberra</option>
<option>Perth</option>
<option>Brisbane</option>
</select>
</form>
</body>
</html>
The example below is aimed at demonstrating drop down select menus. The
alerts (commented out) were added by me.
The example uses arrays within arrays, but doesn't explain them at all well.
So I have some questions:
What syntax would return Australia, France, Japan, New Zealand ?
Why does cities[1][1] not work, but cities['France'][1] does?
Any other useful help with arrays within arrays would be appreciated.
Thanks
MG
=====================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Related Select Test</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
<!--
// Create an array to hold the cities for each country
var cities = new Array(4);
cities["Australia"] =
["Sydney", "Melbourne", "Canberra", "Perth", "Brisbane"];
cities["France"] =
["Paris", "Lyons", "Nice", "Dijon"];
cities["Japan"] = ["Tokyo", "Kyoto", "Osaka", "Nara"];
cities["New Zealand"] =
["Auckland", "Wellington", "Christchurch", "Dunedin", "Queenstown"];
//alert(cities); does not work
//alert(cities[1]); undefined
//alert(cities[1][1]); does not work
//alert(cities['France']); works
//alert(cities['France'][1]); works
function removeOptions(optionMenu)
{
for (var i=0; i < optionMenu.options.length; i++)
optionMenu.options = null;
}
function addOptions(optionList, optionMenu)
{
removeOptions(optionMenu); // clear out the options
for (var i=0; i < optionList.length; i++)
optionMenu = new Option(optionList, optionList);
}
//-->
</script>
</head>
<body>
<h2>Vacation Chooser</h2>
<form name="testform" id="testform" action="#" method="get">
Country:
<select name="country" id="country"
onchange="addOptions(cities[this.options[this.selectedIndex].text],document.testform.city);">
<option selected="selected">Australia</option>
<option>France</option>
<option>Japan</option>
<option>New Zealand</option>
</select>
City:
<select name="city" id="city">
<option>Sydney</option>
<option>Melbourne</option>
<option>Canberra</option>
<option>Perth</option>
<option>Brisbane</option>
</select>
</form>
</body>
</html>