What is the use or difference of combining .off
with .click
?
$('#link').off('click').on('click',function(){});
vs.
$('#link').on('click',function(){});
What are the advantages of combining them?
“Click On” To use “click on” and “click” correctly, use “click on” is for something virtual. Such as a link, a tab, or an app. But use “click” for something physical- such as the right mouse button. However, you can also used “click” for virtual things.
We use .off to unbind (stop) click functionality from all places for this element and then we use .on click to bind only our click functionality. Thanks for contributing an answer to Stack Overflow!
In Jquery, click event occurs when an element clicked and sometimes it will not work if elements are dynamic so better to use onclick when the content is loading dynamically. Eg: click button with id=btnClick to show alert.
For a complete list of shorthand methods, see the events category. Note that .on () differs from .click () in that it has the ability to create delegated event handlers by passing a selector parameter, whereas .click () does not. When .on () is called without a selector parameter, it behaves exactly the same as .click ().
This adds a new click
event handler:
$('#link').on('click', function(){});
This removes all existing click
event handlers that were attached with .on()
(if any) and then adds a new click
event handler:
$('#link').off('click').on('click', function(){});
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