javascript: Unchecking all checkboxes

Update

As per comments, Array.from is not necessary anymore on modern browsers, so you could do

document.querySelectorAll('input[type=checkbox]').forEach(el => el.checked = isAllCheck);

Original answer

After validating that isAllCheck is correct with your UI logic, you may do both with a simple vanilla-js one-liner

Array.from(document.querySelectorAll('input[type=checkbox]')).forEach(el => el.checked = isAllCheck);

The problem boils down to how you're setting the checked property of the checkox. You're assigning a string with "true" where you should be assigning the boolean value true.

So to fix your code is easy:

if( isAllCheck == false ){
    cbarray[i].checked = true;
}else{ 
    cbarray[i].checked = false;
}

or, more succinctly

cbarray[i].checked = !isAllCheck

Live example: http://jsfiddle.net/SEJZP/

Tags:

Javascript