Multiple arguments to a function??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JS

    #1

    Multiple arguments to a function??

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


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

    <select name="sel1" id="sel1" onchange="creat eSecondMenu('se l1','sel2',
    'sel3');">
    </select>

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

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

    </form>



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

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

    function createSecondMen u(sel){
    var s = new Array(); // s holds the menus.
    //s[0] = document.getEle mentById(argume nts[0]); // s[0] contains the first
    menu.
    //s[1] = document.getEle mentById(argume nts[1]); // s[1] contains the second
    menu.
    //s[2] = document.getEle mentById(argume nts[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???



  • Michael Winter

    #2
    Re: Multiple arguments to a function??

    On 31/05/2005 16:13, JS wrote:
    [color=blue]
    > I have this form in test.jsp [...][/color]

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

    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.dem on.co.uk/example_scripts/dependent_selec t.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]
    [color=blue]
    > <select [...] onchange="creat eSecondMenu('se l1','sel2', 'sel3');">
    > </select>[/color]

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

    [snip]
    [color=blue]
    > //s[0] = document.getEle mentById(argume nts[0]); // s[0] contains the
    > first[/color]

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

    [snip]
    [color=blue]
    > 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???[/color]

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

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    Working...