0

i've been developing a rails application for my work, and i wonder how to use coffee script to make my form dynamic.

Here is a sample of my _form.html.erb:

  <tr>
     <td>
      <strong class="col-md-3">L'OCS est-il maintenu ?</strong>
      <div class="col-md-3">
        <%= f.check_box :maintenance, label: "", :class => 'towatch' %>
      </div>
      <strong class="col-md-2">TMA</strong>
      <div class="col-md-3">
        <%= f.collection_select(:tma_id, Entity.all.order(:name), :id, :name, prompt: 'Choisissez une TMA', :class => 'toset', hide_label: true) %>
      </div>
    </td>
  </tr>

I want to make the collection accessible only, if the checkbook is checked.

Here is my coffee script :

# Display if already checked
if $(".towatch:checked")
  $(".toset").toggle()

# Toggle on change
$(".towatch").change ->
  $(".toset").toggle()

This coffee script doesn't work. Can someone explain me why, and how to make it works ?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Takezoshi
  • 104
  • 5
  • Possible duplicate of [Show/hide div if checkbox selected](http://stackoverflow.com/questions/18421082/show-hide-div-if-checkbox-selected) – Jared Smith Jul 27 '16 at 16:09

1 Answers1

0

Finally it was pretty simple, once i get that toggle() wasn't the good function to use, instead I modify css attribut like this:

##### Toggle TMA field if OCS is maintain ####
# Display if already checked
if $('.towatch').prop('checked') == false
  $('.toset').css('display','none')

# Toggle on change
  $('.towatch').change ->
    if $('.towatch').prop('checked')
      $(".toset").css('display','block')
    else
      $('.toset').css('display','none')
Takezoshi
  • 104
  • 5