Monday 26 May 2014

Jquery - check if at least one checkbox is checked 2

     In some situation we need at least one user input in check box to submit form. In that case, we should check if at least one check box is checked. So, we can simply check that condition in .click event listener of check box.

     Here we are going to enable Submit Form button at least any one of check box is checked.

$("input[type='checkbox']").click(function () {
    $("input[type='submit']").attr(
        "disabled", !$("input[type='checkbox']").is(":checked"));
});
     From the above code  !$("input[type='checkbox']").is(":checked") will return switched value of boolean out put. 
i.e.,
If $("input[type='checkbox']").is(":checked") return true, output will be false.
If $("input[type='checkbox']").is(":checked") return false, output will be true.

     Because, if any one of the check box is checked, It will return true. So, we need change button's disabled attribute to false. For that we are using ! to change true to false and false to true .

Here is the DEMO.

Have any doubt, feel free to comment here!

2 comments: