5
\$\begingroup\$

I have a page where a user needs to click two checkboxes before continuing (agree to two terms of service). My jQuery is fairly straightforward, you have to have both check boxes checked to continue:

$("#agree1, #agree2").click(function () {
    if ($("#agree1").is(':checked') == true && $("#agree2").is(':checked') == true) {
        $("#nextPage").removeAttr("disabled");
    } else {
        $("#nextPage").attr("disabled", "disabled");
    }
});

I'm wondering if there is a simpler way of writing this. I expected a line like this to check for both boxes to be checked, but it doesn't:

if ($("#agree1 #agree2").is(':checked') == true)

Is there a way to simplify this?

Example code: jsFiddle

\$\endgroup\$
10
\$\begingroup\$

You can look for only the checked checkboxes, and then check that you found two:

if ($("#agree1:checked,#agree2:checked").length == 2) { ... }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.