title: Select/Deselect All dynamic checkboxes in Rails 4 with Javascript
body: <p>I have followed this StackOverflow Question's <strong><a href="https://stackoverflow.com/questions/8585572/select-all-deselect-all-checkboxes-in-a-page">Answer1</a></strong> and <strong><a href="https://stackoverflow.com/questions/23606222/select-deselect-all-checkboxes">Answer2</a></strong></p>
  
  <p>but not getting result.. As I am calling all checkbox list's data from <code>Range</code> tabel. and their id is also defined by data.. Let me show you the code for this screenshot</p>
  
  <p><img src="https://i.stack.imgur.com/IZVha.png" alt="enter image description here"></p>
  
  <pre><code>= f.collection_check_boxes :range_ids, Range.active, :id, :name, {},:class=&gt;'checkbox'
  </code></pre>
  
  <p>which returns as </p>
  
  <pre><code>&lt;input id="campaign_range_ids_1" class="checkbox" type="checkbox" value="1" name="campaign[range_ids][]"&gt;
  &lt;input id="campaign_range_ids_2" class="checkbox" type="checkbox" value="2" name="campaign[range_ids][]"&gt;
  &lt;input id="campaign_range_ids_3" class="checkbox" type="checkbox" value="3" name="campaign[range_ids][]"&gt;
  &lt;input id="campaign_range_ids_4" class="checkbox" type="checkbox" value="4" name="campaign[range_ids][]"&gt;
  </code></pre>
  
  <p>I want if <code>All</code> means <code>id="campaign_range_ids_4"</code> is selected/deselected then all other check-boxes should be perform accordingly..  </p>
  
  <p>Here also have a screenshot of <strong>Range Table</strong>
  <img src="https://i.stack.imgur.com/woWV3.png" alt="range table"> </p>
  
  <p>I had tried this Javascript</p>
  
  <pre><code>:javascript
    $('#campaign_range_ids_4').click(function() {
         if(this.checked) {
             $('#campaign_range_ids_1').checked = true;
             $('#campaign_range_ids_2').checked = true;
             $('#campaign_range_ids_3').checked = true;
             });
         } else {
             $('#campaign_range_ids_1').checked = false;
             $('#campaign_range_ids_2').checked = false;
             $('#campaign_range_ids_3').checked = false;
             });
         }
      });
  </code></pre>
  
  <p>Please correct me where I do Mistake.... Thanks in advance.. :)</p>
  
  <h2>Updated Question</h2>
  
  <p>I have followed <strong><a href="https://stackoverflow.com/questions/24630578/select-deselect-all-dynamic-checkboxes-in-rails-4-with-javascript#answer-24630749">Shridhar's</a></strong> and <strong><a href="https://stackoverflow.com/questions/24630578/select-deselect-all-dynamic-checkboxes-in-rails-4-with-javascript#answer-24630888">Pavan's</a></strong> answer it works perfectly but now I want that when I select All then deselect any one then it must uncheck "All" (4th checkbox) but it remains as it is..</p>