Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between preventDefault() and off('click')

I use on() JQuery function and preventDefault() for prevent anchor default action. I can not understand off() JQuery function. can I use off('click') instead of:

$('.anchor').on('click', function(e) {
  e.preventDefault();
});

I mean, can I use this for prevent default action of anchor?

$('.anchor').off('click');

1 Answers

No, as per the documentation of .off():

The .off() method removes event handlers that were attached with .on().

So off() only removes handlers that were added by jQuery. But default handlers, e.g. on a link or on a submit button, must be prevented by preventDefault().

like image 173
str Avatar answered Sep 25 '26 21:09

str



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!