Disabling dynamic input with checkbox

I have a checkbox/select and want the select to be enabled when the checkbox is checked and disabled when unchecked.

<p>
<input id="option[21]" type="checkbox" name="checkbox[21]">
<select id="option[21]" name="select[21]" disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

<p>
<input id="option[22]" type="checkbox" name="checkbox[22]">
<select id="option[22]" name="select[22]"  disabled><option value="">-</option><option value="1">Today</option><option value="2">Tomorrow</option></select>
</p>

When I run my code, it doesn't appear to select the dropdown correctly:

$(document).on('change', 'input[type=checkbox]', function(event) {
  var selectId = event.target.id;
  console.log(selectId);

  if ($(this).is(':checked')) {
    $(selectId).removeAttr('disabled');
  } else {
    $(selectId).prop( "disabled", true );
  }
});

https://jsfiddle.net/rL3ghb0f/



Read more here: https://stackoverflow.com/questions/67393458/disabling-dynamic-input-with-checkbox

Content Attribution

This content was originally published by Kermit at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: