# Javascript - multiple select selectboxes

**URL:** <https://www.ruby-forum.com/t/javascript-multiple-select-selectboxes/101937>\
**Category:** Rails\
**Created:** [August 8, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/javascript-multiple-select-selectboxes/101937 "2006-08-08T19:05:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [August 8, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/javascript-multiple-select-selectboxes/101937/1 "2006-08-08T19:05:16Z")

</div>

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?

 \<select name="Division" size="1"

onchange=“setOptions(document.addCodes.Division.options[document.addCodes.Division.selectedIndex].value);”\>  
Autos  
Bobcats

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [August 8, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/javascript-multiple-select-selectboxes/101937/2 "2006-08-08T19:36:47Z")

</div>

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]);  
}  
}

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/javascript-multiple-select-selectboxes/101937/3 "2006-08-15T14:19:18Z")

</div>

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/](http://www.schuerig.de/michael/javascript/)

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
