Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear checkbox

Tags:

I want to clear the selection of checkboxes if they click cancel. How can I achive this with this code:

var chkbox= $('#divListBox').find(':checked')                  .map(function() {                      return $(this).val();                  }).get(); 
like image 418
hersh Avatar asked Aug 16 '10 00:08

hersh


People also ask

How do you clear a check box?

If you want to delete the checkbox, you need to press and hold CTRL, click on the checkbox, and press DELETE on the keyboard.

How do you uncheck a checkbox?

Once the checkbox is selected, we are calling prop() function as prop( "checked", true ) to check the checkbox and prop( "checked", false ) to uncheck the checkbox.

How do I unselect all checkboxes?

Clicking on the master checkbox selects all checkboxes; and unchecking it, deselects all checkboxes.


2 Answers

This should work:

$('#divListBox :checked').removeAttr('checked'); 
like image 60
Thomas Bonini Avatar answered Sep 20 '22 17:09

Thomas Bonini


The advantage of this solution is if you have on change events or on click events tied to the checkbox this will make them happen.

$('#divListBox').each(function(index, element) {   var checked = element.checked;   if (checked) {     $(element).trigger('click');   } }); 

Also with bootstrap the checkbox wasn't getting found with the :checked selector.

Hope that helps.

like image 22
phyatt Avatar answered Sep 18 '22 17:09

phyatt