views:

56

answers:

3

Is there a way for two or more ID's be required to be checked before doing something.

For instance:

If BOTH Checkbox 1 and Checkbox 2 are checked then the event happens.

But if only 1 or the other are checked by themselves, something else happens.

I thought this would work but nope.

function toggleStatus() {
    if ($('#checkbox1 #checkbox2').is(':checked')) {
$('.option1 :input').attr('checked', true);
    } else {
$('.option1 :input').attr('checked', false);
}

function toggleStatus() {
    if ($('#checkbox1').is(':checked')) {
$('.option2 :input').attr('checked', true);
    } else {
$('.option2 :input').attr('checked', false);
}

function toggleStatus() {
    if ($('#checkbox2').is(':checked')) {
$('.option3 :input').attr('checked', true);
    } else {
$('.option3 :input').attr('checked', false);
}

Hopefully I am explaining this correctly. I have looked for three days and I am stuck. Thanks for any help!

+1  A: 

I would give the checkboxes a common class. Then use that as the selector and count the checked values. Then if two are checked do something. If one is checked then check the value of that one and do what you need to accordingly.

EDIT: So say for instance you assigned a common class of myCheckBoxes

So you could do the following pseudo code:

var myCheckBoxes = $('.myCheckBoxes:checked') //not sure on selector

if (myCheckBoxes.length == 2)
    //do something because both are checked
else if (myCheckBoxes.length == 1)
{
    if (myCheckBoxes.val() == "A")
        // do something because A was checked
    else if (myCheckBoxes.val() == "B")
        // do something because B was checked
}
spinon
Assuming `.myCheckBoxes` refers to the checkboxes themselves, you'll need to remove the space from your selector.
patrick dw
@patrick oh yeah didn't notice that. Thanks
spinon
this is a good solution, but it may not the readable. if you dont have a description of what the snippet is trying to do
GerManson
+1  A: 
$('#checkbox1, #checkbox2').change(function() {
   if ($('#checkbox1').is(':checked') && $('#checkbox2').is(':checked')) {
       // Do some stuff if both boxes are checked...
   }
});
Chris Forrette
Thanks. This worked like a charm. I will look into the other solutions when I have more time.
Mark H
A: 
$(document).ready(function() {
   var check1 = $("#check1");
   var check2 = $("#check2");

   // this method decides what to do when a checkbox is clicked
   var trigger_event = function() {
        if (check1.attr("checked") && check2.attr("checked")) {
            event1();
        }
        else if (check1.attr("checked") && !check2.attr("checked")) {
            event2();
        }
        else if (!check1.attr("checked") && check2.attr("checked")) {
            event3();
        }
   };

   // append event
   check1.click(function() {
       trigger_event();
   });

   check2.click(function() {
      trigger_event();
   });

};
GerManson
Thanks A LOT everyone! I will try these and post what I used here.
Mark H
dont forget to mark as an answer the solution that worked best
GerManson