I thought this was supposed to be easy. There are a lot of answers on here about unchecking checkboxes using jQuery but when I try them they don't seem to work. I want to uncheck them as part of my function for resetting the form.
I have a loop and I create checkboxes inside it:
<input type="checkbox" name="is_chosen[]" value="{{ $sample->id }}" class="flat">
Here is the my code for unchecking checkboxes (not all checkboxes are checked initially):
$('input:checkbox').each(function () {
if(this.checked) {
$(this).attr('checked', false);
}
});
I have also tried replacing attr
with prop
. I have also tried using just $(input:checkbox).prop('checked', false);
. I have tried others more but they don't work. What am I missing?
$(input:checkbox).prop('checked', false);
. If it doesn't work check what$(input:checkbox)
selects.class="flat"
(from Bootstrap), although I don't know why exactly. I became worried because none of your answers were working. Then I thought of removing the class. When the class was there, all the checked checkboxes were still checked after executing the function. When I removed the class, the checkboxes became unchecked after performing the function. I tried your answers again and they worked. I tried the code that I posted too and it worked. (Btw, do I have to post this as an answer?)