The script is working fine however I am wondering if there is a way to avoid the repetition in the code (DRY method).
JS code:
// Checkbox checked and input disbaled when page loads
$('#checkbox').prop('checked', true);
if ($('#checkbox').is(':checked') == true) {
$('#textInput').prop('disabled', true);
}
// Enable-Disable text input when checkbox is checked or unchecked
$('#checkbox').change(function() {
if ($('#checkbox').is(':checked') == true) {
$('#textInput').prop('disabled', true);
} else {
$('#textInput').val('').prop('disabled', false);
}
});
If you can't set the attributes by default in HTML
:
// Checkbox checked and input disbaled when page loads
$('#checkbox').prop('checked', true);
// Enable-Disable text input when checkbox is checked or unchecked
$('#checkbox').on('change', function() {
var value = this.checked ? $('#textInput').val() : '';
$('#textInput').prop('disabled', this.checked).val(value);
}).trigger('change');