Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda