2

Is it possible that if one event raise prevent others from raising?

Example on jsFiddle

$(window).click(
    function(e)
    {
        $("#out").html("preventing next event from happening\n");
    });

$(window).click(
    function(e)
    {
        $("#out").html($("#out").html() + "evil event should not write here");
    });
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
BrunoLM
  • 97,872
  • 84
  • 296
  • 452

2 Answers2

4
$(window).click(
    function(e)
    {
        $("#out").html("preventing next event from happening\n");
        e.stopImmediatePropagation();
    });

$(window).click(
    function(e)
    {
        $("#out").html($("#out").html() + "evil event should not write here");
    });

http://api.jquery.com/event.stopImmediatePropagation/ -- read more here

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
gailbear
  • 519
  • 3
  • 13
3

Well, you can use preventDefault() to stop the other event from occurring. This may be overkill for your solution, however, because it doesn't really give you a choice in which event fires.

http://jsfiddle.net/c52Wr/1/

$(window).click(
    function(e)
    {
        $("#out").html("preventing next event from happening\n");
        e.preventdefault();
    });

$(window).click(
    function(e)
    {
        $("#out").html($("#out").html() + "evil event should not write here");
    });

A better solution may be to accept a click and some parameter and check that parameter to decide how you want to respond.

JasCav
  • 34,458
  • 20
  • 113
  • 170