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" data-studentcheckboxid=@space.EventId>
    </div>
  </div>
  <div class="col-md-6 col-sm-6" style="border: 1px solid red;">
    <div class="btn btn-primary disabled registerStudent" data-studentbuttonid=@space.EventId style="padding: 5px; margin: 5px; border: 1px solid black;">
      Register Student
    </div>
  </div>
</div>
user1186050
  • 10,710
  • 18
  • 92
  • 235
  • 1
    possible duplicate of [Disable/enable an input with jQuery?](http://stackoverflow.com/questions/1414365/disable-enable-an-input-with-jquery) – Sabyasachi Mishra Sep 12 '15 at 04:04

3 Answers3

2

Use on change. Click is fired before state change.

polent
  • 21
  • 1
  • 1
    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. – Nomesh DeSilva Sep 12 '15 at 03:57
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

guradio
  • 15,122
  • 3
  • 30
  • 47
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

hammus
  • 2,553
  • 1
  • 15
  • 35