DEV Community

Mahmoud Elmahdi
Mahmoud Elmahdi

Posted on

Are they all checked?

Simplest way to check whether all checkbox input fields checked or not; by converting a NodeList to an Array using ES6 ... spread operator so we can attach every() method to it;

Checkout the Are they all checked? Demo on jsfiddle

[...document.querySelectorAll('input')].every(checkbox => checkbox.checked)
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
ardennl profile image
Arden de Raaij

What would be the benefit for using a spread operator instead of Array.from()?

Collapse
 
mahmoudelmahdi profile image
Mahmoud Elmahdi

Thanks Arden! You can definitely do the same thing with Array.from, but in my case, the ... is better use IMO. Less code, easy to remember, plus performance. Cheers

Collapse
 
ardennl profile image
Arden de Raaij • Edited

Ah btw, I didn't knew Array.from took in two arguments, so an alternative could be

Array.from(document.querySelectorAll('input'), checkbox => checkbox.checked);

Correct me if I'm wrong.

Collapse
 
ardennl profile image
Arden de Raaij

I kinda like Array.from() because it's so verbose and easy readable, but I guess that's also because not everyone (read: me) is used to spread operators yet. Thanks for the clarification!

Collapse
 
joelnet profile image
JavaScript Joel

Also

Array.prototype.every.call(document.querySelectorAll('input'), checkbox => checkbox.checked)