234

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!

hygull
  • 8,464
  • 2
  • 43
  • 52
Gary
  • 4,495
  • 13
  • 36
  • 49
  • 2
    possible duplicate of [jQuery: Find All Unselected Checkboxes, How To](http://stackoverflow.com/questions/1093350/jquery-find-all-unselected-checkboxes-how-to) – Felix Kling Dec 11 '11 at 17:10
  • 2
    Possible duplicate of [How to find all unselected checkboxes?](https://stackoverflow.com/questions/1093350/how-to-find-all-unselected-checkboxes) – jacefarm Oct 14 '17 at 20:02
  • @FelixKling This is clearly an exact duplicate. Please swing your jquery hammer. – mickmackusa Jul 17 '20 at 05:58

8 Answers8

500

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

$("input:checkbox:not(:checked)")
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
36

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

dave
  • 12,406
  • 10
  • 42
  • 59
16

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

var matches = document.querySelectorAll('input[type="checkbox"]:not(:checked)');
Pavlo Hryza
  • 627
  • 1
  • 11
  • 18
10

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.

Tobias Wicker
  • 617
  • 8
  • 12
Thulasiram
  • 8,432
  • 8
  • 46
  • 54
3
$("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 (,).
Travis
  • 12,001
  • 8
  • 39
  • 52
cdub
  • 31
  • 1
  • 2
    You shouldn't be including something in your answer that is completely irrelevant for the OP. He doesn't use a `chkAll` box so what's the point of mentioning it. – MMM May 24 '13 at 16:36
3

You can use like this :

$(":checkbox:not(:checked)")
Ehsan
  • 12,655
  • 3
  • 25
  • 44
  • 1
    Why repeat the guidance already provided years earlier? Furthermore, please never post code-only snippets on SO. All answers should be explained. – mickmackusa Jul 17 '20 at 05:19
3

To select by class, you can do this:

$("input.className:checkbox:not(:checked)")
Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
  • 1
    The question asked does not have a `class` attribute. This is an unnecessary answer since the suggestion of `:checkbox:not(:checked)` was given **YEARS** earlier. – mickmackusa Jul 17 '20 at 05:18
-1
$(".clscss-row").each(function () {
if ($(this).find(".po-checkbox").not(":checked")) {
               // enter your code here
            } });
  • 2
    Welcome to StackOverflow! Please add an explanation to your code, so other users (for whom this might be what they're looking for) know what your code does. – Nander Speerstra Dec 20 '17 at 07:40