Jake B Jake B - 9 days ago 6
HTML Question

How to disable / enable an input field with a checkbox

Current code:

<form>
<div class="form-group" id="deadline" >
<input type="text" class="datepicker form-control" id="deadline" name="deadline" placeholder="DD/MM/YYYY" >
<input type="checkbox" value="ongoing" name="Ongoing" id="ongoing" >
<label for="ongoing">Ongoing</label>
</div>
</form>


how do i make it so that checking the "Ongoing" checkbox disables the "deadline" input field?
Thanks ahead of time.

Answer

Using jQuery would be easiest way in my opinion

$('#ongoing').change(function() {
  if(this.checked) {
    $('#deadline').prop('disabled',true);
  } else {
    $('#deadline').prop('disabled',false);
  }
});
Comments