Onclick checkbox uncheck other checkboxes

I have 6 flags, one for each business day and one that says "everything."

What I want to do is uncheck all the other fields if someone clicks the "all" checkbox, if that makes sense.

For example, if someone clicked Monday and Wednesday ... then they went in and checked the "all" box, and then checked the box on Monday and Wednesday.

Greetings

+3
source share
2 answers

This is not what you want, but it seems more reasonable.

HTML

<input type="checkbox" id="chkAll" />
<br />
<input type="checkbox" id="chkMonday" class="child" />
<input type="checkbox" id="chkTuesday" class="child" />
<input type="checkbox" id="chkWednesday" class="child" />
<input type="checkbox" id="chkThursday" class="child" />
<input type="checkbox" id="chkFriday" class="child" />
<input type="checkbox" id="chkSaturday" class="child" />
<input type="checkbox" id="chkSunday" class="child" />

JQuery

$(function(){
    $("#chkAll").change(function(){
        if (this.checked) {
            $("input:checkbox.child").attr("checked", "checked");
        }
        else {
            $("input:checkbox.child").removeAttr("checked");
        }
    });
});

See working demo

See the updated version , which also handles changes to child flags.

+4

jquery , , .

$('#all-id').change(function(){
   if($('#all-id').is(':checked')){
     if($('#monday-id').is(':checked')){
        $('#monday-id').attr('checked', false);
     } else {
       $('#monday-id').attr('checked', true);
     }
     // etc
   }
});

, ,

+1

Source: https://habr.com/ru/post/1780347/


All Articles