Shermaine Chaingan Shermaine Chaingan - 5 months ago 15
jQuery Question

Checkbox disable when amount in textbox exceed

I have this code and working properly however when i clear the text field the checkbox remain checked what should i do ?

<form name="form1">
<div class="controlset-pad">
<input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" disabled='disabled'/>
</div>
<input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" disabled='disabled'/>
</div>


$('input[name=other_name]').keyup(function(){
if (this.value.length > 0) {
$('input[name="others"]').prop('disabled', false)
} else {
$('input[name="others"]').prop('disabled', true)
}
})


http://jsfiddle.net/H8VPY/47/

Answer

Change your script as follows. It simply unchecks before disabling the control.

$('input[name=other_name]').keyup(function(){
    if (this.value.length > 0) {
       $('input[name="others"]').prop('disabled', false)
    } else {
       $('input[name="others"]').attr('checked', false);
       $('input[name="others"]').prop('disabled', true)      
    }
})