Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Is it possible to remove one specific event handler from an event with multiple handlers?

As i searched for the solution of a problem i have right now, i found this thread: jQuery: more than one handler for same event. Now i wonder if it is possible to remove just a single one of these handlers?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It is possible, if you don't use anonymous callbacks:

var $elt = $(...);

function clickHandler1(event){...}
function clickHandler2(event){...}

// bind both
$elt.click(clickHandler1);
$elt.click(clickHandler2);

// unbind just the first
$elt.unbind('click', clickHandler1);

A wild Demo appeared!

See also: .unbind() docs.

over 4 years ago · Santiago Trujillo Relatório

0

Actually, you can do it using anonymous callbacks via event namespacing:

$elt.bind('click.handler1', function(event) {
    alert('click handler 1');
});

$elt.unbind('click.handler1');

See modified demo here.

And here for more information.

This was added in jQuery 1.4.3, which actually came out more than 4 months before the question was asked.

over 4 years ago · Santiago Trujillo Relatório

0

This post is ages old, so I thought I'd chime in with how this is done today (2019).

Most of the old answers above, including the accepted answer (which was correct when it was written), use the unbind method which has been deprecated since jQuery v.3.0.

As of jQuery 3.0, .unbind() has been deprecated. It was superseded by the .off() method since jQuery 1.7, so its use was already discouraged.

Source

Here's how to do it with the .off() method:

var foo = function() {
  // Code to handle some kind of event
};

// ... Now foo will be called when paragraphs are clicked ...
$( "body" ).on( "click", "p", foo );

// ... Foo will no longer be called.
$( "body" ).off( "click", "p", foo );

Source

This will work for all event handlers implemented with the .on() method and it works for undelegated event handlers as well. Simply replace "body" with your element selector and remove the second parameter of the .off() method ("p" in the example above).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda