# How to pass selected checkboxes from form when a select element changes?

**URL:** <https://www.ruby-forum.com/t/how-to-pass-selected-checkboxes-from-form-when-a-select-element-changes/178275>\
**Category:** Rails\
**Created:** [November 2, 2009, 12:29am UTC](https://www.ruby-forum.com/t/how-to-pass-selected-checkboxes-from-form-when-a-select-element-changes/178275 "2009-11-02T00:29:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [November 2, 2009, 12:29am UTC](https://www.ruby-forum.com/t/how-to-pass-selected-checkboxes-from-form-when-a-select-element-changes/178275/1 "2009-11-02T00:29:22Z")

</div>

I have a list that is enclosed in a form. Each item in the list has a  
checkbox, so an excerted bit of the code is (the part to list the  
contacts is really in a partial):

\<% form\_tag fxn\_for\_selected\_contacts\_path, :method =\> :put do %\>

```
...

<%= for contact in @contacts do %>
  <%= check_box_tag('contact_ids[]', contact.id, false, :class =>

```

‘selector’) %\>  
\<%= contact.name %\>  
\<% end %\>

…

\<% end %\>

The form also has a select menu that is used to list a set of ‘groups’  
that the selected items can be added to. I’d like to add the selected  
items to the selected group either using an :onchange handler or  
observe\_field when the user changes the menu selection.

At the moment I’m calling a remote\_function like so:

:onchange =\> remote\_function(:url =\> {:action  
=\> :add\_to\_group}, :with =\> “‘value=’ + value”)

this works well to pass the selected group in params[:value]

My problem is that because I’m calling remote\_function, I dont get the  
contact\_ids[] passed back, so I dont know what the selections are in  
the add\_to\_group action.

Any thoughts on how to determine which checkboxes are selected and  
pass them to this remote function?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [November 2, 2009, 3:50am UTC](https://www.ruby-forum.com/t/how-to-pass-selected-checkboxes-from-form-when-a-select-element-changes/178275/2 "2009-11-02T03:50:12Z")

</div>

OK! I figured it and and share below to help anyone else who comes  
across this problem…

I created a JS fxn to translate the selected checkboxes into query  
string parameters that Rails will read as params.

function getSelections(collectionName, className)  
{  
paramsString = “”  
boxes = document.getElementsByClassName(className);  
for (i = 0; i \< boxes.length; i++)  
if (boxes[i].checked)  
{  
paramsString += “&” + collectionName + “[]=” + boxes[i].value;  
}  
return paramsString ;  
}

note that you pass in the name of the array of checkboxes as you’d  
like to pick it up in the params hash in your controller. Also, you  
pass the class name that you gave to the checkboxes in your view so  
that the fxn can pick them up from the DOM.

So, for my example, this is how I use all this in my view for the  
select menu:

\<%= select\_tag :selected\_group,  
options\_from\_collection\_for\_select(@groups, ‘id’, ‘name’),  
:onchange =\> remote\_function(:url =\> {:action  
=\> :add\_to\_group}, :with =\> “‘value=’ + value + getSelections  
(‘contact\_ids’, ‘selector’)”) %\>

so, the controller picks up the select menu’s choice in params[:value]  
and the selected objects from the list in params[:contact\_ids].

Hope that helps someone.
