0

I have a checkbox and want to call a function when I check the checkbox and also disable that function when I uncheck the checkbox. How do I do it?

Note: By disable I mean, it should revert the process done by that function.

Update:
Is there any way to directly disable the function that I called? Since it contains many other click events inside it. So I don't wanna turn those off individually. I just wanna disable that function so that those click events automatically goes off.

3 Answers 3

1
 $('#checkbox').change(function() {
    if($(this).is(":checked")) {
        // do something  if checked
    }
    else{
       // do something if not checked
    }       
});
Sign up to request clarification or add additional context in comments.

2 Comments

or simply this.checked
@VishnuChaudhry You welcome. Please do consider accept this answer so that other can follow this one.
0

Listen to the change event.

$('input#your-input-id').change(function() {
     if(this.checked) {
         // call the function
     }
    else {
         // call "undo" function
    }

 });

Comments

0
$('input[type="checkbox"]').on('change', function() {
    if(this.checked) {
        // process if checked
    } else {
        // revert process here
    }
    // example to set variable:
    var myvar = this.checked ? "It is checked" : "not checked";
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.