Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable, Enable on('click')

Tags:

jquery

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?

like image 360
Nomistake Avatar asked Nov 07 '12 18:11

Nomistake


People also ask

How do I disable on click event?

To disable the click event in HTML, the “pointer-events” property of CSS is used. For this purpose, add an HTML element and set the value of the pointer-events property as “none” to disable its click event. This article explained how to disable the click event using CSS along with its example.

Can disabled buttons be clicked?

A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How to disable and enable click event in jQuery?

prop('checked')) $('#submit'). off('click'); else $('#submit'). on('click', clickButton); }); }); That will take care of enabling/disabling click event for button.

Does disabled prevent onClick?

The disabled attribute prevents the onClick event from firing.


1 Answers

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
like image 127
Selvakumar Arumugam Avatar answered Sep 22 '22 03:09

Selvakumar Arumugam