Home > database >  Disabled attribute not being removed when checkbox is unchecked with jQuery
Disabled attribute not being removed when checkbox is unchecked with jQuery

Time:06-03

The script below will add the disabled attribute to other input fields when #alt_hp field is checked, but when I uncheck the field, the disabled attribute does not get removed. I am not that versed in javascript and tried to make sense of other solutions on this site.

<script>
$("#alt_hp").change(function() {
    if ($("input[type=checkbox]").is( ":checked" )) {
        $("#new_displacement").val('')
        $("input#induction, input#heads, input#camshaft, input#turbo, input#boost, input#inc_displacement").prop("checked", false).prop("disabled", true)
    } else {
        $("input#induction, input#heads, input#camshaft, input#turbo, input#boost, input#inc_displacement").prop("disabled", false)
    }
});
</script> 

CodePudding user response:

Just change below line

if ($("input[type=checkbox]").is( ":checked" )) {

to this

if ($(this).is(":checked")) {

Your existing code will always refer to any/all checkbox in the whole html. this refers to the current checkbox/element.

  • Related