views:

23

answers:

1

How can I say if you click on checkbox2 or checkbox3 and select one of them, then force checkbox1 to be selected as well

$('input:checkbox').click(function() {
 if ($(this).attr('class') == 'checkbox2' || $(this).attr('class') == 'checkbox3')
  {   
    if($(this).attr('checked',true))
    {
      $(this).parent().....
    }
}

});

<TD> 

<div class='checkboxes'>
<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8253]" value="36" class="checkbox1" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8253]" value="14" class="checkbox2" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8253]" value="14" class="checkbox3" >
</LABEL><BR>     

</div>
</TD>

<TD> 

<div class='checkboxes'>
<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8251]" value="36" class="checkbox1" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8251]" value="14" class="checkbox2" >
</LABEL><BR>     

<LABEL style="white-space: nowrap;">
<INPUT type="checkbox" name="user_registered[8251]" value="14" class="checkbox3" >
</LABEL><BR>     

</div>
</TD>
+1  A: 

Basically, you can put a click function onto checkbox2 and checkbox3 so that, when they are clicked, they check to see if they are now selected. If so, then checkbox1 should be selected.

Of course, you can add additional work to unselect checkbox1 if both are unselected, but that wasn't specified in your question. (Just add an else if to the statement.)

$('input.checkbox2, input.checkbox3').click(function() {
    if ($(this).is(':checked')) {
        $(this).siblings('input.checkbox1').attr('checked', true);  
    }
});

If the checkbox2 and checkbox3 aren't direct siblings, you may need to utilize the name so that you can properly select the correct checkbox1 which is easy enough to do. My brain is fried, though, and I need sleep. Hope this helps!

JasCav
sure this is valid syntax?if ($(this):checked) {
bud
also note that there are many checkbox1;s
bud
@bud - I plead being exhausted. Updating now.
JasCav
still missing one more bracket , but I get the idea.. thanks,,, go to sleep now!
bud
@bud - Roger that. And, if my answer helped you, feel free to accept it. Night!
JasCav