views:

1135

answers:

3

In my Rails app I've created a set of checkboxes as follows:

<div class="form_row">
     <label for="features[]">Features:</label>
     <% ['scenarios', 'news', 'role_profiles', 'private_messages', 'chatrooms', 'forums', 'polls'].each do |feature| %>
     <br><%= check_box_tag 'features[]', feature, (@features || {}).include?(feature) %>
     <%= feature.humanize %>
     <% end %>
</div>

I'd like to know how to create a button that would "Select All".

+1  A: 

If you use the Prototype JS, you might find this blog post helpful. It gives a fairly concise way to perform a select all.

http://www.ryboe.com/2008/07/10/select-all-checkboxes-with-prototype-js.html

In your view you could use the following snippet to create a "Select All" link:

<%= link_to_function("Select All","checkboxes.each(function(e){ e.checked = 1 })") %>

In addition, you'd need the following Javascript code somewhere on the same page (or maybe even abstracted out to the public/javascripts/application.js file

var checkboxes = [];
checkboxes = $$('input').each(function(e){ if(e.type == 'checkbox') checkboxes.push(e) });
var form = $('options'); /* Replace 'options' with the ID of the FORM element */
checkboxes = form.getInputs('checkbox');

Here's the full source of a working example, if this doesn't work you might need to check to make sure your JS libraries are loading properly.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <title></title>
    <script type="text/javascript" src="http://www.google.com/jsapi" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
      var checkboxes;
      google.load("prototype", "1.6");
      google.setOnLoadCallback(function(){
        checkboxes = [];
        checkboxes = $$('input').each(function(e){ if(e.type == 'checkbox') checkboxes.push(e) });
        var form = $('options'); /* Replace 'options' with the ID of the FORM element */
        checkboxes = form.getInputs('checkbox');
      });
    </script>
  </head>
  <body>
    <form id="options">
      <fieldset><input type="text" value="test"></fieldset>
      <fieldset><input type="checkbox" value=0> 0</fieldset>
      <fieldset><input type="checkbox" value=1> 1</fieldset>
      <fieldset><input type="checkbox" value=2> 2</fieldset>
      <fieldset><input type="checkbox" value=3> 3</fieldset>
    </form>
    <a href="#" onclick="checkboxes.each(function(e){e.checked = 1})">Select All</a>
  </body>
</html>
Scott
It doesn't work. Have you tested it?
alamodey
Added working example. Should be easy to replace the link w/ "link_to_function" in Rails
Scott
+1  A: 

Using jQuery;

<script type="text/javascript">
      function selectAll(){
        $("input:checkbox").each(function(){
          $(this).attr('checked', true);
          return false;
        });
      }
</script>

HTML button:

<a href="#" onclick="selectAll()">Select All</a>
revgum