hersh
hersh

Reputation: 1183

how to clear checkbox

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();

Upvotes: 24

Views: 44639

Answers (3)

Nikhil VJ
Nikhil VJ

Reputation: 6112

You have tagged the question with jquery but not explicitly mentioned that you only want to use jquery, so...

document.getElementById("divListBox").checked = false;

Frankly I only use jquery when it helps.

Upvotes: 0

phyatt
phyatt

Reputation: 19102

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.

Upvotes: 1

Andreas Bonini
Andreas Bonini

Reputation: 44742

This should work:

$('#divListBox :checked').removeAttr('checked');

Upvotes: 56

Related Questions