lundi 13 mars 2017

jquery checkbox disable/uncheck

I have 2 checkboxes as below. The 2nd one should be clickable only if the 1st one checked. This I managed to do.

But if the first one is unchecked, the second one should be disabled and unchecked as well.

$(document).ready(function() {
  var ckbox = $('#optin');

  $('input').on('click', function() {
    if (ckbox.is(':checked')) {
      $('#tc').removeAttr('disabled'); //enable input

    } else {
      $('#tc').prop('checked', false); //remove check
      $('#tc').removeAttr('checked'); //disable input

    }
  });
});
<script src="http://ift.tt/1oMJErh"></script>
<input type="checkbox" name="optin" id="optin" class="required">
<label for="optin"> Checkbox1 </label>

<input type="checkbox" name="tc" id="tc" class="" disabled="disabled">
<label for="tc"> Checkbox2  </label>



Aucun commentaire:

Enregistrer un commentaire