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?
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.
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.).
prop('checked')) $('#submit'). off('click'); else $('#submit'). on('click', clickButton); }); }); That will take care of enabling/disabling click event for button.
The disabled attribute prevents the onClick event from firing.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With