Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
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');
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

off : disable the eventHandler so no function can be execute on click preventdefault : disable the default eventHandler but you can use your own function.

over 4 years ago · Santiago Trujillo Report

0

They are logically very different.

preventDefault() stops the browser from acting on the event behaviour but still executes the JS code. For example, in the below, the URL the .anchor element points to will not be transferred, however the JS code will still run:

$('.anchor').on('click',function(e){
    e.preventDefault();
    console.log('the browser wont redirect, but you will see this line in the console')
});

On the other hand, off() completely removes any event handler of the given type from the element. In this case, the browser will be transferred to the given URL on click and no JS code will be run.

$('.anchor').off('click');
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!