Gary
Gary

Reputation: 4725

Find all unchecked checkboxes in jQuery

I have a list of checkboxes:

<input type="checkbox" name="answer" id="id_1' value="1" />
<input type="checkbox" name="answer" id="id_2' value="2" />
...
<input type="checkbox" name="answer" id="id_n' value="n" />

I can collect all the values of checked checkboxes; my question is how can get all the values of unchecked checkboxes? I tried:

$("input:unchecked").val();

to get an unchecked checkbox's value, but I got:

Syntax error, unrecognized expression: unchecked.

Can anybody shed a light on this issue? Thank you!

Upvotes: 242

Views: 233887

Answers (8)

Thulasiram
Thulasiram

Reputation: 8552

You can do so by extending jQuerys functionality. This will shorten the amount of text you have to write for the selector.

$.extend($.expr[':'], {
        unchecked: function (obj) {
            return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
        }
    }
);

You can then use $("input:unchecked") to get all checkboxes and radio buttons that are checked.

Upvotes: 10

Chukwuemeka Inya
Chukwuemeka Inya

Reputation: 2671

To select by class, you can do this:

$("input.className:checkbox:not(:checked)")

Upvotes: 4

Pavlo Hryza
Pavlo Hryza

Reputation: 667

Also it can be achieved with pure js in such a way:

var matches = document.querySelectorAll('input[type="checkbox"]:not(:checked)');

Upvotes: 18

UTHIRASAMY
UTHIRASAMY

Reputation: 1

$(".clscss-row").each(function () {
if ($(this).find(".po-checkbox").not(":checked")) {
               // enter your code here
            } });

Upvotes: -1

Ehsan
Ehsan

Reputation: 12951

You can use like this :

$(":checkbox:not(:checked)")

Upvotes: 4

cdub
cdub

Reputation: 31

$("input[type='checkbox']:not(:checked):not('\#chkAll\')").map(function () { 
   var a = ""; 
   if (this.name != "chkAll") { 
      a = this.name + "|off"; 
   } 
   return a; 
}).get().join();

This will retrieve all unchecked checkboxes and exclude the "chkAll" checkbox that I use to check|uncheck all checkboxes. Since I want to know what value I'm passing to the database I set these to off, since the checkboxes give me a value of on.

//looking for unchecked checkboxes, but don’t include the checkbox all that checks or unchecks all checkboxes
//.map - Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.

//.get - Retrieve the DOM elements matched by the jQuery object.
//.join - (javascript) joins the elements of an array into a string, and returns the string.The elements will be separated by a specified separator. The default separator is comma (,).

Upvotes: 3

SLaks
SLaks

Reputation: 887305

As the error message states, jQuery does not include a :unchecked selector.
Instead, you need to invert the :checked selector:

$("input:checkbox:not(:checked)")

Upvotes: 512

dave
dave

Reputation: 12806

$("input:checkbox:not(:checked)") Will get you the unchecked boxes.

Upvotes: 37

Related Questions