Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between .on('click') and .off('click').on('click')

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?

like image 538
Jithin Raj P R Avatar asked Jul 17 '17 05:07

Jithin Raj P R


People also ask

What is the difference between click on and click on?

“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.

What is the difference between on click and off click bind?

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!

When to use onclick vs click event in jQuery?

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.

What is the difference between click() and on() event methods?

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 ().


1 Answers

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(){});
like image 60
Racil Hilan Avatar answered Oct 27 '22 12:10

Racil Hilan