Vishnu Chaudhry
Vishnu Chaudhry

Reputation: 1

How to call a function using checkbox in jQuery?

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.

Upvotes: 0

Views: 61

Answers (3)

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

$('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";
});

Upvotes: 0

Scarecrow
Scarecrow

Reputation: 4137

 $('#checkbox').change(function() {
    if($(this).is(":checked")) {
        // do something  if checked
    }
    else{
       // do something if not checked
    }       
});

Upvotes: 1

asiviero
asiviero

Reputation: 1235

Listen to the change event.

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

 });

Upvotes: 0

Related Questions