Bogdan Popa
Bogdan Popa

Reputation: 1099

Turn simple form checkbox into a toggle switch

I have a few checkboxes inside a simple_form_for:

<%= f.input :agree, label: "Agree with terms and conditions*" %>

which outputs the following html:

<div class="form-group boolean optional enrollment_agree">
  <div class="checkbox">
    <input value="0" type="hidden" name="enrollment[agree]">
    <label><input class="boolean optional" type="checkbox" value="1" name="enrollment[agree]" id="enrollment_agree"></label>
  </div>
</div>

I found a nice and simple material design toggle switch http://codepen.io/chrisota/pen/jWmqvx which works with the following html:

<input id="toggle" type="checkbox" class="hide"/>
<label for="toggle"><span class="hide">Label Title</span></label>

How can I make it work within the rails form?

Upvotes: 0

Views: 2106

Answers (1)

Anthony E
Anthony E

Reputation: 11245

Add a hidden field to your form and update the value of the hidden with javascript when the toggle occurs.

javascript:

$('#toggle').change(function(evt) {
   $('#hidden_field').value = $(this).value();
});

Upvotes: 1

Related Questions