jonmrich
jonmrich

Reputation: 4333

Check if specific set of checkboxes are checked

I'm trying to figuring out how to see if a certain set of checkboxes are checked. I can get my script to work to check if a single box, specific box is checked, but can't figure out the more complex case.

Checkboxes:

<input type="checkbox" id="option1" value="1" >
<input type="checkbox" id="option2" value="2">
<input type="checkbox" id="option3" value="3">

The relevant part of the jquery:

if ($('input[id=option1]').is(':checked')) {
     //do something
}

Basically, what I want to do is this (which I know doesn't work):

if ($('input[id=option1]').is(':checked')) AND ($('input[id=option2]').is(':checked')) {
     //do something
}

How do I accomplish something like this? I need to be able to check a if a number of different combinations are true. Any help would be appreciated.

Thanks.

Upvotes: 1

Views: 66

Answers (2)

Dhruva Sagar
Dhruva Sagar

Reputation: 7307

You could also just selected all checked inputs with this :

$('input[type="checkbox"]:checked')

And do what you need only with them.

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

you can create a set first and then check if the count of elements is the same as the count of the checked elements

var group = $('#option1,#option2,#option3');

if (group.length === group.filter(':checked').length){
  // all are checked so you can do something..
}

Upvotes: 2

Related Questions