prop('disabled', false); is not working

Dmytro Petrenko picture Dmytro Petrenko · Oct 10, 2017 · Viewed 9k times · Source

I want to make a button that will be disable when checkbox is false. But prop('disabled', false); isn't working.

Answer

adiga picture adiga · Oct 10, 2017

In your code, this refers to the DOM.

Change it to

$("#agree").change(function() {
    if ($(this).is(':checked')) {
        $('#button').prop('disabled', false);
    } else {
        $('#button').prop('disabled', true);
    }
});

Inside the event handler, this refers to the element that triggered the change event.

or simply:

$("#agree").click(function() {
    $('#button').prop('disabled', !$(this).is(':checked'));
});