1

I'm trying to enable and disable a button when a checkbox gets checked, but can't seem to get it working. The button doesn't enable when I check the checkbox.

$("input[type=checkbox]").on("click", function() {
  var id = $(this).data("studentcheckboxid");
  var button = $("div").find("[data-studentbuttonid='" + id + "']");
  $(button).prop('disabled', false);
});
<div class="row registerColumns">
  <div class="col-md-6 col-sm-6" style="border: 1px solid brown;">
    <div id="acceptBox" style="padding: 5px; margin: 5px; border: 1px solid black;">
      Accept
      <input type="checkbox" class="studentCheckbox" [email protected]>
    </div>
  </div>
  <div class="col-md-6 col-sm-6" style="border: 1px solid red;">
    <div class="btn btn-primary disabled registerStudent" [email protected] style="padding: 5px; margin: 5px; border: 1px solid black;">
      Register Student
    </div>
  </div>
</div>

1

3 Answers 3

2

Use on change. Click is fired before state change.

Sign up to request clarification or add additional context in comments.

1 Comment

welcome to community. please associate your answer with some code segment. you may read the stackoverflow help guide on how to write a good answer.
1
$("input[type=checkbox]").on("click", function () {
    var id = $(this).data("studentcheckboxid");
    var button = $("div").find("[data-studentbuttonid='" + id + "']");

    if ($(this).is(':checked')) {
        $(button).prop('disabled', true);
    } else {
        $(button).prop('disabled', false);
    }
});

Although i cant find any button i made a fiddle and made a button myself you can check it here

Comments

0

   $("input[type=checkbox]").on("change", function() {
  var id = $(this).attr('data-studentcheckboxid');
  var button = $("button[data-studentbuttonid='" + id + "']")
  .prop('disabled', function(i, v) { return !v; });
});
<div class="row registerColumns">
  <div class="col-md-6 col-sm-6" style="border: 1px solid brown;">
<div id="acceptBox" style="padding: 5px; margin: 5px; border: 1px solid black;">
  Accept
  <input type="checkbox" class="studentCheckbox" data-studentcheckboxid="SomeId">
</div>
  </div>
  <div class="col-md-6 col-sm-6" style="border: 1px solid red;">
<div class="btn btn-primary disabled registerStudent" data-studentbuttonid="SomeId" style="padding: 5px; margin: 5px; border: 1px solid black;">
  Register Student
</div>
  </div>
</div>

<button data-studentbuttonid="SomeId" style="height: 20px; width: 50px;" disabled>Button</button>

Working Fiddle

2 Comments

If you click again the button will not be disable. more like toggle
Right you are, sir. Updated.

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.