Hello!

Help me figure out why flag selection does not work in the example I found:

<script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready( function() { $("#maincheck").click( function() { if($('#maincheck').attr('checked')){ $('.mc').attr('checked', true); } else { $('.mc').attr('checked', false); } }); }); </script> <input type="checkbox" name="numbers[]" class="mc" value="0" /> <input type="checkbox" name="numbers[]" class="mc" value="1" /> <input type="checkbox" name="numbers[]" class="mc" value="2" /> <input type="checkbox" name="numbers[]" class="mc" value="3" /> <input type="checkbox" name="numbers[]" class="mc" value="4" /> <input type="checkbox" name="numbers[]" class="mc" value="5" /> <input type="checkbox" name="numbers[]" class="mc" value="6" /> <input type="checkbox" name="numbers[]" class="mc" value="7" /> <input type="checkbox" name="numbers[]" class="mc" value="8" /> <input type="checkbox" name="numbers[]" class="mc" value="9" /> <br /><input type="checkbox" name="maincheck" id="maincheck" /> 

Task: select all checkboxes for the class .mc, if the box is in #maincheck. Removing the checkboxes works, but for some reason there is no selection (

I suspect that the function for the removal of the latest version of jquery has changed, and this code was written it is not known when. Help, pliz.

    1 answer 1

    Do not attr, but use prop.

      $(document).ready( function() { $("#maincheck").click( function() { if($('#maincheck').prop('checked')){ $('.mc').prop('checked', true); } else { $('.mc').prop('checked', false); } }); //Добавлено $('.mc').click(function(){ if($('.mc:not(:checked)').length==0){ $('#maincheck').prop('checked',true); }else{ $('#maincheck').prop('checked',false); } }); }); 

    http://jsfiddle.net/atcjjbqw/

    PS Although attr works in some browsers, its use here is not entirely correct .

    • Oh, thank you, everything is super, now it worked))) - oleg777
    • @ oleg777, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - knes
    • Done) Thank you, I didn’t know that this was the way to do) - oleg777
    • Look at the code after "// added". This is so that the common jackdaw is snapped out when the children are snapping. - knes
    • Oh, thank you very much, once again))) - oleg777