Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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

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

to check for both boxes to be checked, but it doesn't.

Is there a way to simplify this?

Example code: jsFiddle

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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

if ($("#agree1:checked,#agree2:checked').length == 2) {
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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