Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  160] [ 1]  / answers: 1 / hits: 26379  / 10 Years ago, wed, july 16, 2014, 12:00:00

I am using jquery 1.11.1 and this is my code:



$(#rowchkall).change(function(){
if($(this).is(':checked')){
$(input:checkbox[class=rowchk]).each(function() {
alert(set checked);
$(this).attr('checked', checked);
});
}else{
$(input:checkbox[class=rowchk]).each(function() {
$(this).attr('checked', false);
});
}
});


When I first click on #rowchkall, all the checkbox is set to checked. When I click it again, all checkbox is unchecked.



When I click it again, alert box still appear but none of the checkbox will be checked. Why is it only work for first time only? How can I fix it?



Thank you.


More From » jquery

 Answers
28

Use .prop() instead of .attr()



$(this).prop('checked', true); //true to check else false uncheck


Your code can be simplified as



$(#rowchkall).change(function () {
$(input:checkbox.rowchk).prop('checked',this.checked);
});


A Good Read .prop() vs .attr()


[#70190] Monday, July 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
viridianaw

Total Points: 154
Total Questions: 94
Total Answers: 89

Location: South Georgia
Member since Sun, Aug 8, 2021
3 Years ago
;