When you click on the checkbox, the change event should work. In which all other checkboxes should become unckecked. When you click nothing happens, put a breakpoint, does not even enter. What exactly is wrong? Thanks in advance for your reply.

I use to display checkboxes library https://github.com/fntneves/jquery-labelauty

projects.coffee

jQuery(document).on 'turbolinks:load', -> $('.control__check').labelauty checked_label: "Cancel", unchecked_label: "Select", force_random_id: true $('.control__check').change -> thisElement = $(this) if ($(this).prop("checked")) $('ul.content-list content-list__my-response content-list_responses'). find('.control__check labelauty').each (index, elem) -> if ($(elem).prop("checked") && !thisElement ) elem.prop('checked', false) 

_responses.html.erb

 <ul class="content-list content-list__my-response content-list_responses"> <% @project.responses.each do |response| %> <% if response.persisted? %> ................................................................ <div class="controls"> <%= check_box_tag '', true, false, { class: "control__check" } %> </div> ................................................................. </li> <% end %> <% end %> </ul> 

UPDATED 06/14/16:

This is the HTML checkbox source code:

 <div class="controls"> <input type="checkbox" name="dfvfvfdvf" id="labelauty-179303" value="true" class="control__check labelauty" style="display: none;"> <label for="labelauty-179303"><span class="labelauty-unchecked-image</span> <span class="labelauty-unchecked">Select</span> <span class="labelauty-checked-image"></span> <span class="labelauty-checked">Cancel</span></label> </div> 

    2 answers 2

    To be honest, some kind of strong witchcraft to switch. I suspect that this is the case:

    $ ('ul.content-list content-list__my-response content-list_responses')

    find ('. control__check labelauty')

    In both cases, the selector is wrong. There are no labelauty , content-list_responses and content-list__my-response tags in HTML. Apparently, by design, these should be classes, and selectors for classes begin with . . Details here. If you really want to hang an event to change the original checkboxes - you can take the original selector: $('.control__check')

    But why so hard? Judging by the examples of this plugin, he knows how to work with radio inputs. Accordingly, the bike is not needed:

     <% @project.responses.each do |response| %> <%= radio_button_tag 'my_radio', true, false, { class: "control__check" } %> <% end %> $('.control__check').labelauty checked_label: "Cancel", unchecked_label: "Select", force_random_id: true 

    Should work as needed

    Note: the name attribute becomes mandatory. Without it, shoals with groups can begin.

    • Thanks for the answer. I take the original selector - '.control__check'). change -> ... See my script. When changed, it does not enter the event handler code at all. The fact is that by default checkboxes can be all not active. Accordingly, the radio button is not suitable. - Stefan Hansch
    • @StefanHansch, I agree, hurried. And can you see what coffee is compiling? - anoam
    • @StefanHansch radio beats can be turned off in the initial state. The only difference is that after selecting back to this state they cannot be brought. Of course, if this plugin does not further limit them. - D-side
    • @anoam I added the source code to the html checkbox. - Stefan Hansch
    • @StefanHansch, super! Only I asked for js , which came out of the presented coffee . - anoam

    In the original html checkbox line:

     <label for="labelauty-179303"><span class="labelauty-unchecked-image</span> 

    Somewhat not valid. Migration error or html generator error?

    If the error was in generating the writing of html, then maybe javascript stumbles at the moment?

    When changing html to valid, at least an alert is triggered in the change function:

    https://jsfiddle.net/nxnpn20j/3/

    The only thing that was changed was the rewrite of the code from the turbolinks event: load to $ (document) .ready

    • Take a closer look, everything is valid. Your answer is rather a question. Next time write similar messages in the comments to the question. - ilyaplot
    • @ilyaplot no, you look closer. - Pavel Mayorov
    • Yes, I apologize, there are problems - ilyaplot
    • I apologize, did not figure out to the end with how to properly interact with the answers / comments. The system does not write to me in the comment to the question - alexander fedorov