Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily deactive onchange event

Could someone tell me how you can deactivate the event onchange and reactivate it

 $(':checkbox').each(function (k, v) {
     $(this).onchange = null;//
     $(this).off() // works but I do not know how to  reactivate onchange event
 })

Edit 1: Let me explain you my problem ... I'm using bootstrap toggle onmy checkbox. But maybe this is only a detail.

SO my problem is .. I have a "reset" button on my form. When I click on it, I have a function who loops on each checkbox of my form trying to "reset" each checkbox. By reseting my checkbox I mean setting them to their initial value. . . but doing so I trigger an "onchange" function present later in my code. And I do not want to trigger that "onchange" function

like image 895
zm455 Avatar asked Aug 11 '16 15:08

zm455


1 Answers

I would recommend one of two options.

First, you can unbind and rebind the handlers by giving the function a name:

function handleCheckboxClick(e) {
    //...
}

$(':checkbox').off('change');
$(':checkbox').on('change', handleCheckboxClick);

The other option is to create a variable that you set to true or false as needed to specify whether to execute the handler:

$(':checkbox').on('change', function(e) {
    if (!window.shouldExecuteCheckboxChangeHandler)
        return;

    //...
});
like image 74
Jason P Avatar answered Oct 17 '22 22:10

Jason P