Multiple arguments to a function??

J

JS

I have this form in test.jsp, that consists of 3 drop/down menus (sel1, sel2
and sel3):


<body onload="createFirstMenu('sel1');">
<form name="sels" method="post" action="">

<select name="sel1" id="sel1" onchange="createSecondMenu('sel1','sel2',
'sel3');">
</select>

<select name="sel2" id="sel2" onchange="createThirdMenu('sel1','sel2',
'sel3');">
<option value="0">-- V&aelig;lg --</option>
</select>

<select name="sel3" id="sel3" onchange="update('sel1', 'sel2', 'sel3');">
<option value="0">--V&aelig;lg--</option>
</select>

</form>



In a javaScript I have the functions: createFirstMenu, createSecondMenu,
createThirdMenu and update.

If I choose something from sel1 "createSecondMenu" gets called with 3
arguments:

function createSecondMenu(sel){
var s = new Array(); // s holds the menus.
//s[0] = document.getElementById(arguments[0]); // s[0] contains the first
menu.
//s[1] = document.getElementById(arguments[1]); // s[1] contains the second
menu.
//s[2] = document.getElementById(arguments[2]); // s[2] contains the third
menu.
sel = s[1]; // sel now contains the second menu.


var choice1 = s[0].selectedIndex; // The index in the first menu.
var choice2 = s[1].selectedIndex; // The index in the second menu.
var choice3 = s[2].selectedIndex; // The index in the third menu.
resetMenu(sel);
}

But I just realized that choice1-3 contains the selected index from the 3
menus even when out comment the lines where I grap the arguments.

How can s[0], s[1] and s[2] contain the selected index from the right
drop/down menus when they don't contain a SELECT object???
 
M

Michael Winter

I have this form in test.jsp [...]

Instead of constantly posting untestable fragments of code, post a URL
to a (semi-)workable page that can be examined properly.
<body onload="createFirstMenu('sel1');">

If this is for the Web, you should probably give this endeavor up:
constructing forms in this way is not a good way to author for a public
audience.

Richard Cornford has a method for producing dependent SELECT elements
without server-side support.

<URL:http://www.litotes.demon.co.uk/example_scripts/dependent_select.html>

Of course, if you have server-side support (and you do), then you should
just use that instead. Granted, it requires round trips to the server,
but at least it's reliable and doesn't force the user to download
options they may never need to select.

[Musing]
It would be nice if OPTGROUP elements could be nested. That would
produce a much nicer scriptable structure, and possibly help with
usability, too.
[/Musing]
<select [...] onchange="createSecondMenu('sel1','sel2', 'sel3');">
</select>

SELECT elements are required to have at least one OPTION element.

[snip]
//s[0] = document.getElementById(arguments[0]); // s[0] contains the
first

Stop using the getElementById method to access form controls. Use the
forms and elements collections instead.

[snip]
How can s[0], s[1] and s[2] contain the selected index from the right
drop/down menus when they don't contain a SELECT object???

They don't. Common sense should tell you that. The script will (and
does) error out.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,192
Latest member
KalaReid2

Latest Threads

Top