Reputation: 635
I can disable the textbox when the checkbox is selected, but I can't figure out how to clear the textbox if anything is in it.
<input type="text" id="form_sc1"/>
<input type="checkbox" id="form_setchange"/>
JQuery
$(document).ready(function(){
if($('input.form_setchange').is(':checked')){
$("#form_sc1").attr("disabled", "disabled");
}
Upvotes: 0
Views: 3398
Reputation:
Try this
$(document).ready(function(){
$('input:checkbox').on('click', function(){
if($('input:checkbox').is(':checked')){
$('input:text').prop('disabled', 'disabled');
$('input:text').val('');
}
else{
$('input:text').prop('disabled', false);
}
});
});
Working JSFiddle here https://jsfiddle.net/86Lv2urz/1/
Upvotes: 1
Reputation: 135
Can you try below code once
$('#form_setchange').change(function() {
var $check = $(this);
if ($check.prop('checked')) {
$("#form_sc1").val('');
$("#form_sc1").attr("disabled", "disabled");
} else {
$("#form_sc1").removeAttr("disabled");
}
});
Hope it may helps...
Upvotes: 0
Reputation: 1792
if($('input.form_setchange').is(':checked')){
$("#form_sc1").prop("disabled", true).val('');
}
Upvotes: 0
Reputation: 941
You can also try this-
$('#form_setchange').attr("value", "");
OR
$('#form_setchange').val('');
Upvotes: 0
Reputation: 10177
try this:
$('input:checkbox').on('click', function(){
$('input:text').prop('disbaled', 'disabled');
$('input:text').val('');
});
Upvotes: 0