I have a check box and a text field in a form. If the checkbox is checked, I want to disable the field (and if it's unchecked, I want it to be enabled).
I tried to write some jQuery based on the second answer for this question: jQuery - checkbox enable/disable
However, the text field is (foo_price) is disabled when the page loads even though the field is unchecked. What am I doing wrong?
I'm using the Simple Form gem:
<%= simple_form_for @foo do |f| %>
<%= f.input :free_flag, as: :boolean %>
<%= f.input :price %>
<%= f.button :submit %>
<% end %>
The form renders:
<form>
<input class="boolean optional" type="checkbox" value="1" name="foo[free_flag]" id="foo_free_flag" />
<input class="numeric decimal optional" type="number" step="any" value="12.99" name="foo[price]" id="foo_price" />
</form>
My Coffeescript:
enable_cb = ->
$('#foo_price').prop 'disabled', !@checked
return
$ ->
enable_cb()
$('#foo_free_flag').click enable_cb
return