Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  181] [ 3]  / answers: 1 / hits: 23055  / 8 Years ago, tue, may 10, 2016, 12:00:00

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?


More From » jquery

 Answers
27

I'm sorry for the inconvenience guys.



I found out that the problem was 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. So it was the class all along.


[#62239] Sunday, May 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adilene

Total Points: 395
Total Questions: 88
Total Answers: 109

Location: Indonesia
Member since Tue, Aug 3, 2021
3 Years ago
;