1

i have disabled all the checkboxes except one. On click of that checkbox i want to enable all the checkbox. and if that checkbox is unchecked then all other checkboxes should remain disabled. Could anybody please help me with this. I have tried to use

$(document).ready(function() {<br>
    if ($('#mainCheckbox').is(':checked')) {<br>
     $(".otherCheckbox").removeAttr("disabled");
     }      
});

But this isn't working for me.

3 Answers 3

5

This works fine for me

<input type="checkbox" id="chkMain" /><br
<input class="child" type="checkbox" id="chk1" disabled="true" />
<input class="child" type="checkbox" id="chk2" disabled="true" />
<input class="child" type="checkbox" id="chk3" disabled="true" />
<input class="child" type="checkbox" id="chk4" disabled="true" />
<input class="child" type="checkbox" id="chk5" disabled="true" />

$(function(){
  $("#chkMain").click ( function() {

    if ( !$(this).is ( ":checked" ) )
    {
      $(".child").attr ( "disabled" , true );
    }
    else
    {
      $(".child").removeAttr ( "disabled" );
    }
  });
});

Working Demo

If you can post the HTML also then it would be more helpful.

Sign up to request clarification or add additional context in comments.

Comments

1

this is working:

if($("#mainCheckbox").is(":checked")){
    $("input:checkbox").not(this).removeAttr("disabled");
}
else{
    $("input:checkbox").not(this).attr("disabled","true");
}

Comments

0

Here's a solution (note the each function):

function toogleOthersIf(aBoolean) {
    $(".otherCheckBox").each(function() {
        if (aBoolean) {
            $(this).removeAttr("disabled");
        } else {
            $(this).attr("disabled", "disabled");
        }
    });
}

$(document).ready(function(){
    $('#mainCheckBox').click(function(e) {
        toogleOthersIf($('#mainCheckBox:checked').length > 0);
    });

    toogleOthersIf(false);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.