I am trying to populate a multiple select box using javascript, but am having problems with the name. When I change the name to "codes[]" I can''t get the javascript to find the form element, but without the [] on the end, rails wont read all of the selected values. Here is an example of the javascript working, but when you submit the array is not passed across for the codes. Anyone work with this sort of thing before? <html> <head> <SCRIPT language="JavaScript"> function setOptions(chosen) { var selectBox = document.addCodes.codes; selectBox.options.length = 0; var len = data[chosen].length; for(i=0; i<len; i++) { selectBox.options[selectBox.options.length] = new Option(data[chosen][i], data[chosen][i]); } } data = new Array(); data[''00''] = new Array(); data[''00''][0] = "Autos - AU001"; data[''00''][1] = "Autos - AU003"; data[''00''][2] = "Autos - AU004"; data[''00''][3] = "Autos - AU005"; data[''00''][4] = "Autos - AU006"; data[''00''][5] = "Autos - AU007"; data[''01''] = new Array(); data[''01''][0] = "BobCat - BC103"; data[''01''][1] = "BobCat - BC104"; data[''01''][2] = "BobCat - BC105"; data[''01''][3] = "BobCat - BC106"; data[''01''][4] = "BobCat - BC107"; </SCRIPT> </head> <body> <form name="addCodes" action="test/update" method="post"> <select name="Division" size="1" onchange="setOptions(document.addCodes.Division.options[document.addCodes.Division.selectedIndex].value);"> <option value="00">Autos</option> <option value="01">Bobcats</option> </select> <select name="codes" size="10" multiple> </select> <input type="submit" value="Add" /> </form> </body> </html> -- Posted via http://www.ruby-forum.com/.
Must be the 5 minute rule, after I posted this I figured it out. function setOptions(chosen) { var formObj = document.addCodes; var selectBox = formObj.elements[''codes[]''] selectBox.options.length = 0; var len = data[chosen].length; for(i=0; i<len; i++) { selectBox.options[selectBox.options.length] new Option(data[chosen][i], data[chosen][i]); } } -- Posted via http://www.ruby-forum.com/.
Michael Schuerig
2006-Aug-08 23:57 UTC
[Rails] Re: Javascript - multiple select selectboxes
On Tuesday 08 August 2006 19:36, Bob wrote:> Must be the 5 minute rule, after I posted this I figured it out.[snip] I''ve got a few functions of this kind in my ProtoPlus library. If you''re interested, have a look at http://www.schuerig.de/michael/javascript/ Michael -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/