mercredi 16 décembre 2015

Rails f.check_box hide/show JS

I am having trouble with hiding the default label on my checkboxes in my Rails form. I have a custom label and no matter what I try I always get the column name displayed. I am also trying to use this checkbox as not only a model submission but to trigger a div hide/show event, and I cant seem to get that working either.

<div class="form-group">
    <label class="col-sm-4 control-label">Minor?:</label>
    <div class="col-sm-8">
        <%= f.check_box :minor %>
    </div>
</div>

Here is my hide/show js

<script type="text/javascript">
  var checkbox = document.getElementById('customers_minor');
  var details_div = document.getElementById('minor_details');
  checkbox.onchange = function() {
     if(this.checked) {
       details_div.style['display'] = 'block';
     } else {
       details_div.style['display'] = 'none';
     }
  };
</script>

EDIT/SOLUTION I have figured out how to add the custom label by adding do to the label line. <%= f.check_box :minor do %>Custom Label<% end %>

<label for="customer_minor">
    <input name="customer[minor]" type="hidden" value="0">
    <input id="customer_minor" name="customer[minor]" type="checkbox" value="1">
Custom Label
</label>

Aucun commentaire:

Enregistrer un commentaire