Search code examples
javascriptjqueryeventschainingpreventdefault

How to prevent child event from firing in JQuery


So i have a button

<input id="expandButton" type="submit" value="Expand" class="loadWindow"/>

I have two event handler attached.

function confirmContinue() {
     return confirm("Do you want to expand window, might reload some information?");
}

$("input.loadWindow").click(function(event) {
      showProcessingWindow(event);
}

$("#expandButton").click(function(event) {
     var result = confirmContinue();
     if (!result) {
          event.preventDefault();
          event.stopPropagaton();
     }
}

I want to prevent "input.loadWindow" click event from firing if they say cancel.

This is what is happening right now. Button clicked --> Confirmation fire --> I click cancel --> show processing window still fires.

I want it so that Button clicked --> Confirmation fire --> i click cancel --> do nothing.


Solution

  • It looks like event.stopImmediatePropagation() would work in this case.

    $("#expandButton").click(function(event) {
         var result = confirmContinue();
         if (!result) {
              event.preventDefault();
              event.stopImmediatePropagation(); 
         }
    }
    

    Fiddle demo- http://jsfiddle.net/w2vwn35x/1/