Search code examples
javascriptjqueryeventsmouseup

stop propagation after body mouseup event with jquery?


Have two events:

 $('body').mouseup(function(e){}

and

$('.toggle').click(function(e){}

I only want one of these to trigger. I tried

 e.stopPropagation(); e.preventDefault(); return false; 

but that doesn't seem to stop it. Any ideas? the mouseup is triggered first.

Thanks.


Solution

  • The problem is they are two different events. If you want to prevent the body.onmouseup, you need to add something on .toggle to catch/stop mouseup events; like

    $('.toggle').mouseup(function(e){
        e.stopPropagation();
        e.preventDefault();
        // these two are older ways I like to add to maximize browser compat
        e.returnValue = false;
        e.cancelBubble = true;
        return false; 
    }