Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all checkboxes are unchecked

I am trying to make a function to check if all checkboxes are unchecked. I have a similar function for text boxes. As I have not worked with checkboxes much before, I'm not sure how to adapt it except for changing input[type=text] to input[type=checkbox].

Can anyone help me out? Thanks.

var textinputs = document.querySelectorAll('input[type=text]'); 
var empty = [].filter.call( textinputs, function( el ) {
     return !el.value
});

    if (textinputs.length == empty.length) {
        alert("None filled");
        return false;
}
like image 954
user2014429 Avatar asked Feb 10 '13 18:02

user2014429


People also ask

How check if checkbox is unchecked?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do I uncheck the select all checkbox?

Clicking on the master checkbox selects all checkboxes; and unchecking it, deselects all checkboxes.

How do I check all check boxes?

In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.


3 Answers

The following should do the trick:

var textinputs = document.querySelectorAll('input[type=checkbox]'); 
var empty = [].filter.call( textinputs, function( el ) {
   return !el.checked
});

if (textinputs.length == empty.length) {
    alert("None filled");
    return false;
}
like image 125
endyourif Avatar answered Oct 17 '22 01:10

endyourif


You can simplify a little, given that you're able to use querySelectorAll():

var checked = document.querySelectorAll('input:checked');

if (checked.length === 0) {
    // there are no checked checkboxes
    console.log('no checkboxes checked');
} else {
    // there are some checked checkboxes
    console.log(checked.length + ' checkboxes checked');
}

JS Fiddle (with no checkboxes checked).

JS Fiddle (with some checkboxes checked).

Or, if all you want is a Boolean value to indicate whether any checkbox is checked, for use in a function:

var isChecked = document.querySelectorAll('input:checked').length === 0 ? false : true;
return isChecked;

Proof-of-concept demo.

You could, of course, avoid creating a variable and simply return the result of the ternary; I only used the variable to try and make it clear what, precisely, I was returning/testing-for.

Reference:

  • :checked pseudo-class.
like image 28
David Thomas Avatar answered Oct 17 '22 02:10

David Thomas


Here, a short and very simple example (Vanilla Javascript):

if (document.querySelectorAll('input[type="checkbox"]:checked').length === document.querySelectorAll('input[type="checkbox"]').length) {
    console.log('All checkboxes are checked');
} else {
    console.log('Some checkboxes are not checked');
}

Here in jQuery syntax:

if ($('input[type="checkbox"]:checked').length === $('input[type="checkbox"]').length) {
    console.log('All checkboxes are checked');
} else {
    console.log('Some checkboxes are not checked');
}

Another way to do it, with the :not() pseudo-selector:

if (document.querySelectorAll('input[type="checkbox"]:not(:checked)').length) {
    console.log('Some checkbox are not checked');
}
like image 5
Zlitus Avatar answered Oct 17 '22 02:10

Zlitus