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

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 16, 2013 at 20:04
\$\endgroup\$

1 Answer 1

10
\$\begingroup\$

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

if ($("#agree1:checked,#agree2:checked").length == 2) { ... }
answered Sep 16, 2013 at 21:52
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.