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");
    });
BrunoLM
  • 88,362
  • 76
  • 272
  • 427

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
  • 88,362
  • 76
  • 272
  • 427
gailbear
  • 479
  • 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
  • 33,714
  • 19
  • 103
  • 163