javascript help on array preload
Posted: Fri Feb 13, 2004 11:12 am
i have a javascript that updates menu 2 when you pick a country from menu 1. it works fine once the page is loaded but when the page first loads up menu 2 is blank if menu 1 has an option in menu 1 selected, i want menu 2 to preload the values in menu 2. i dont want to use the onload in the <body> tag. is there away to load menu 2 with the options when the page first loads?
heres my code. so if UK is selected when the page loads, as in the code i want 'Mortlake' to display, so i do not need to pick USA and then UK again to display the options in UK.
<script>
var browser_type=navigator.appName
var co4 = new Array(
"Mortlake")
var co3 = new Array(
"1","Maplewood")
function updateList(theForm, catName, subjListLength){
for (var i=subjListLength + 1 ; i > 0 ; i-- )
{
theForm.header_lid.options = null
}
if( catName == ""){
var option0 = new Option("Territory", "")
}
else{
eval('var option0 = new Option("Territory", "")')
}
NSL=0
if (catName != "None") {
eval("var NewSource = " + "co"+ catName)
NSL = NewSource.length
for(var L = 0; L < NSL + 1; L++) {
eval("var option" + [L + 1] + " = new Option(" + '"' + NewSource[L] + '", "' + NewSource[L] + '")')
}
}
for (var i=0; i < NSL + 1; i++) {
eval("theForm.header_lid.options=option" + i)
if (i==0) {
theForm.header_lid.options.selected=true
}
}
if (browser_type=="Netscape")
{ history.go(0) }
}
</script>
this is the form
<form action="home.php" method="post">
country:
<select name="header_cid" class="NavDD" onChange="updateList(this.form, this.options[selectedIndex].value, this.form.header_lid.length)">
<option value="">Country</option>
<option value="4" selected>United Kingdom</option>
<option value="3" >USA</option>
</select>
territory:
<select name="header_lid" class="NavDD" id="header_lid">
<option value="" selected>Territory</option>
</select>
</form>
heres my code. so if UK is selected when the page loads, as in the code i want 'Mortlake' to display, so i do not need to pick USA and then UK again to display the options in UK.
<script>
var browser_type=navigator.appName
var co4 = new Array(
"Mortlake")
var co3 = new Array(
"1","Maplewood")
function updateList(theForm, catName, subjListLength){
for (var i=subjListLength + 1 ; i > 0 ; i-- )
{
theForm.header_lid.options = null
}
if( catName == ""){
var option0 = new Option("Territory", "")
}
else{
eval('var option0 = new Option("Territory", "")')
}
NSL=0
if (catName != "None") {
eval("var NewSource = " + "co"+ catName)
NSL = NewSource.length
for(var L = 0; L < NSL + 1; L++) {
eval("var option" + [L + 1] + " = new Option(" + '"' + NewSource[L] + '", "' + NewSource[L] + '")')
}
}
for (var i=0; i < NSL + 1; i++) {
eval("theForm.header_lid.options=option" + i)
if (i==0) {
theForm.header_lid.options.selected=true
}
}
if (browser_type=="Netscape")
{ history.go(0) }
}
</script>
this is the form
<form action="home.php" method="post">
country:
<select name="header_cid" class="NavDD" onChange="updateList(this.form, this.options[selectedIndex].value, this.form.header_lid.length)">
<option value="">Country</option>
<option value="4" selected>United Kingdom</option>
<option value="3" >USA</option>
</select>
territory:
<select name="header_lid" class="NavDD" id="header_lid">
<option value="" selected>Territory</option>
</select>
</form>