0

Visit my fiddle first

I am using jQuery-ui button set here. What I want when both check-boxes are checked I need the buttons to be activate and removeClass disabledButton, and when not checked (both) I want the buttons to be disabled and addClass disabledButton. How can I do that?

Here is my jQuery approach:

$(".conditionCheck").buttonset();    
$(".conditions").click(function () {    
  if ($(".conditions").is(':checked')) {   
    $(".paymentButton").removeAttr("disabled")
      .removeClass("disabledButton");   
  }    
  if (!$(".conditions").is(':checked')) {   
    $(".paymentButton").attr("disabled")
      .addClass("disabledButton");    
  } 
});

1 Answer 1

2

Use $('.conditions').not(':checked').length to get the number of unchecked controls. If this number is 0 then both have been checked, so you know what to do.

$(".conditions").click(function () {
  if ($('.conditions').not(':checked').length) {
    $(".paymentButton").prop("disabled", true).addClass("disabledButton");
  }
  else {
    $(".paymentButton").prop("disabled", false).removeClass("disabledButton");
  }
});

I have also changed the incorrect use of attr to disable the elements with prop which is the appropriate way.

The above code can also be written more concisely as

var allChecked = $('.conditions').not(':checked').length == 0;
$(".paymentButton").prop("disabled", !allChecked)
                   .toggleClass("disabledButton", !allChecked);
Sign up to request clarification or add additional context in comments.

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.