jQuery Multiple Event Handlers - How to Cancel?

36,329

Solution 1

Use the stopImmediatePropagation function of the jQuery event object.

Keeps the rest of the handlers from being executed. This method also stops the bubbling by calling event.stopPropagation().

$(document).click(function(event) { 
  alert('a');
  event.stopImmediatePropagation();
  return false;
});

$(document).click(function() {
  alert('b');
});

Solution 2

The first thing I'm asking is: why do you have two functions bound to the same click event? Having access to the code, why don't you just make that one single call?

$(function (){
    var callbackOne = function(e){
        alert("I'm the first callback... Warning, I might return false!");
        return false;
    };

    var callbackTwo = function(e){
        alert("I'm the second callback");
    };

    $(document).click(function (e){
        if(callbackOne(e)){
            callbackTwo(e);
        }
    });
});

Solution 3

Thanks, unbind works for redefining functions.

  $(document).ready(function(){
        $("#buscar_mercaderia").click(function(){ alert('Seleccione Tipo de mercaderia'); 
   });
$(".tipo").live('click',function(){
            if($(this).attr('checked')!=''){
                if($(this).val()=='libro'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){  window.open('buscar_libro.php','Buscar Libros', 'width=800,height=500'); });
                }else if($(this).val()=='otra'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){ window.open('buscar_mercaderia.php','Buscar Mercaderias', 'width=800,height=500');  });
                }
            }
        })

Solution 4

Does using unbind help?

$(document).click(function() { 
  alert('a');
  $(this).unbind('click');
  return false;
});
Share:
36,329
Josh Stodola
Author by

Josh Stodola

I make things happen. I am a seasoned web developer with 15+ years of experience. I am fluent in CSS and Javascript. On the server-side, I prefer the Microsoft stack (.NET) but am not opposed to anything that works. I have ten years of experience working with legacy IBM mainframe systems, which helps me appreciate how far we have come as software engineers. Twitter " There are any number of ways to do this, and most of them are wrong "

Updated on September 09, 2020

Comments

  • Josh Stodola
    Josh Stodola over 3 years

    I have two functions bound to a click event at two different times (using jQuery). The order in which they are fired is significant. They are firing in the correct order. The problem is, when the first function returns false, the second function is still firing!

    How can I properly cancel the event?

    Example code:

    $(document).click(function() { 
      alert('a');
      return false;
    });
    
    $(document).click(function() {
      alert('b');
    });
    

    You will still see the "b" alert message when clicking the page. This is unacceptable!