How to make a button that will cancel all the checkboxes?

I have this code that you can check / uncheck all the checkboxes and check / uncheck the box.

  <!DOCTYPE HTML>
 <html>
   <head>
   <meta charset="utf-8">
 <title>Persist checkboxes</title>
</head>
<body>
<div>
  <label for="checkAll">Check all</label>
  <input type="checkbox" id="checkAll">
</div>
<div>
  <label for="option1">Option 1</label>
  <input type="checkbox" id="option1">
</div>
<div>
  <label for="option2">Option 2</label>
  <input type="checkbox" id="option2">
</div>
<div>
  <label for="option3">Option 3</label>
  <input type="checkbox" id="option3">
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.cookie/1.4.0/jquery.cookie.min.js"></script>

<script>
  $("#checkAll").on("change", function() {
    $(':checkbox').not(this).prop('checked', this.checked);
  });

  $(":checkbox").on("change", function(){
    var checkboxValues = {};
    $(":checkbox").each(function(){
      checkboxValues[this.id] = this.checked;
    });
    $.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' })
  });

  function repopulateCheckboxes(){
    var checkboxValues = $.cookie('checkboxValues');
    if(checkboxValues){
      Object.keys(checkboxValues).forEach(function(element) {
        var checked = checkboxValues[element];
        $("#" + element).prop('checked', checked);
      });
    }
  }

  $.cookie.json = true;
  repopulateCheckboxes();
</script>

My question is that I want to create a button that will have a function that clears or deselects all the checkboxes. Can someone help me do this? Thank!

+4
source share
7 answers

The following function will be called up when the button is pressed:

function uncheckAll(){
   $('input[type="checkbox"]:checked').prop('checked',false);
}

function uncheckAll() {
  $("input[type='checkbox']:checked").prop("checked", false)
}
$(':button').on('click', uncheckAll)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' />
<input type='checkbox' />
<input type='checkbox' />
<input type='checkbox' />
<input type='checkbox' />
<input type='button' value="clear" />
Run codeHide result
+7
source
<button type="reset">Uncheck all</button>
+3
source

http://jsfiddle.net/techsin/Zq6Et/

$('#btn').click(function(){
    $("input[type='checkbox']").attr("checked",false);
});
+3

:

1) Check all Checkboxes
1) UnCheck all Checkboxes

JS Fiddle

+2

http://jsfiddle.net/fh2nV/1/

Html:

<table border="1">
<tr>
    <th><input type="checkbox" id="selectall"/></th>
    <th>Cell phone</th>

</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="1"/></td>
    <td>BlackBerry Bold 9650</td>

</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="2"/></td>
    <td>Samsung Galaxy</td>

</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="3"/></td>
    <td>Droid X</td>

</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="4"/></td>
    <td>HTC Desire</td>

</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="5"/></td>
    <td>Apple iPhone 4</td>
    </tr>
</table>

JQuery

$(function(){

    // add multiple select / deselect functionality
    $("#selectall").click(function () {
          $('.case').attr('checked', this.checked);
    });

    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".case").click(function(){

        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }

    });
});
+1

reset :

<input type="reset" />

Fiddle


:

$('#unchk').click(function(){
    $('input[type="checkbox"]').each(function(){
    $(this).attr("checked",false);
    });
});

Fiddle 2

0

Use a bit of jQuery to achieve what you want to do.

$("input[type='checkbox']").is(":checked").attr("checked",false)

That should work. It is not verified, although it cannot be syntactically correct. Just add to the click listener for any item.

0
source

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


All Articles