kwk.stack
kwk.stack

Reputation: 553

two check boxes that are interdependent

i have two check boxes that when clicked individually perform there tasks but if both are clicked then there is an other task that should be perform but i am facing an issue.

when i click only 1st check box it should do this. and when i click only 2nd check box it should do this but when i click one and then other and both are checked now do this. i need to do this in jquery

here is the jsFiddle

if(checkbox1) is checkd 
{
   //do this
}

if(checkbox2) is checkd 
{
   //do this
}

if(both) are checked do this

here is the html

<input type="checkbox" id="checkbox1" class="kk" />
<input type="checkbox" id="checkbox2" class="kk" />

Upvotes: 1

Views: 175

Answers (3)

sangram parmar
sangram parmar

Reputation: 8726

try this

if ($('#checkbox1').is(':checked') && $('#checkbox2').is(':checked')) {
    // if both are checked
}
else if ($('#checkbox1').is(':checked')) {
    // if checkbox1 are checked
}
else if ($('#checkbox2').is(':checked')) {
    // if checkbox2 are checked
}

Upvotes: 1

Simon M
Simon M

Reputation: 2941

Try this code:

var oneChecked = jQuery('#checkbox1').is(':checked');
var twoChecked = jQuery('#checkbox2').is(':checked');
if (oneChecked && twoChecked) {
    // both are checked
} else if (oneChecked) {
    // checkbox1 is checked
} else if (twoChecked) {
    // checkbox2 is checked
} else {
    // nothing is checked
}

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

Try

if($("#checkbox1 input:checked") && $("#checkbox2 input:checked")){
--

}
else if($("#checkbox1 input:checked"){
 ---
}

else if($("#checkbox2  input:checked"){
 ---
}else{
-------------nothing checked
}

Upvotes: 1

Related Questions