Page 1 of 1

javascript help on array preload

Posted: Fri Feb 13, 2004 11:12 am
by gurjit
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>

Posted: Wed Feb 18, 2004 11:24 am
by tylerdurden
You could simply call your function at the end of the script, after the function definition. No need to use onLoad.

Are you sure the script works apart from this problem? I didn't get it to run in Mozilla or Safari.

Check out http://www.titleofsite.com/foo.html for a similar script I prepared.

Posted: Wed Feb 18, 2004 11:28 am
by gurjit
nice one seems to look like exactly what i need.

my current script does work but i might just use yours and change it as required.

Thanks for your reply. If i have any problems i'll get in touch.

Thanks again.

Posted: Wed Feb 25, 2004 11:45 am
by gurjit
tylerdurden i been using the script you gave me. i'm having problems.

i push the ids dynamically from a database, it is not neccessary that my id values in the first select box or second, third select box are going to be uniform i.e. 1,2,3 they could be 3,2,6,77 in select box 1 and 3,5,99,44 in select box 2. you use uniform values to loop your array. how can i get around this?

Posted: Wed Feb 25, 2004 3:53 pm
by tylerdurden
I wrote this a while ago and need to clean it up for more general use. Do you have 2 or 3 select boxes? I'll write you a cleaned up version once I know.

Posted: Thu Feb 26, 2004 3:04 am
by gurjit
I have a strange menu system. depending on which user level logs in the menus can range from one select box to 3 select boxs. so i need to be able to put a if statement around how many select boxes to display.

i have one select box with countries listed, second with territories and the third with schools.

if a principal logs in the only territory and school display.
if a country level logs in then country territory and school display.

Also i noticed that the second or third select box does not accept 'selected' values unless they exists as a key in the first select box. How can i make the selected value in the second and third select box?

i would appreciate if you could clean it up and send me a copy. thank you for all your help.

Posted: Sat Feb 28, 2004 6:39 am
by tylerdurden
Here you go:

http://www.titleofsite.com/selects/foo_new.html

with three selects.

http://www.titleofsite.com/selects/foo_new2.html

with two selects.

http://www.titleofsite.com/selects/foo_new4.html

with four selects.

Could be prettier I think, but it's cleaned up and it works. (Haven't tested it in IE Windows, could you give it a try?)

Posted: Tue Mar 02, 2004 10:15 am
by gurjit
thanks for doing this for me tylerdurden. really appreciated. thanks a million. it works great. i like the different samples.

works well in IE 6 in windows for which i need it for.

THANK YOU.