7

How can i disable and enable a on click event.

I tried with:

$('#web').on('click', function web_function(event){
    event.stopPropagation();
    // execute a bunch of action to preform
});
$('#web').off('click'); // click is succesfully removed
$('#web').on('click'); // doesnt work, i need to redefine the actions to perform

Also I tried disabling with:

$('#web').unbind('click'); // click is succesfully removed
$('#web').bind('click'); // nok

But this also doesnt work...

So, i would like to disable/enable the click event without the need to redefine the actions to perform. Some sort of toggle... (click on/off)

And how do i implement the event to stop propagation?

How can i do this?

Nomistake
  • 893
  • 2
  • 17
  • 32

2 Answers2

16

You need to pass the handler function as the argument whenever you bind (or rebind).

In your case, you can name the function which you can use to pass it any time you re-bind again.. see below,

var myFunc = function(event){
     event.stopPropagation();
     // execute a bunch of action to preform
}

$('#web').on('click', myFunc); //bind myFunc
$('#web').off('click'); // click is succesfully removed
$('#web').on('click', myFunc); //rebind again
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
3

first disable the element using prop() method of jQuery, i.e.

$("selector").prop("disabled",true);

and when you want to enable click the just do it by using

$("selector").prop("disabled",false);
Syscall
  • 19,327
  • 10
  • 37
  • 52
Kaleem Nalband
  • 687
  • 7
  • 21