What is the difference between event.stopPropagation and event.stopImmediatePropagation?

Technology CommunityCategory: jQueryWhat is the difference between event.stopPropagation and event.stopImmediatePropagation?
VietMX Staff asked 3 years ago

event.stopPropagation() allows other handlers on the same element to be executed, while event.stopImmediatePropagation() prevents every event from running. For example, see below jQuery code block.

$("p").click(function(event){
  event.stopImmediatePropagation();
});
$("p").click(function(event){
  // This function won't be executed
  $(this).css("background-color", "#f00");
}); 

If event.stopPropagation was used in previous example, then the next click event on p element which changes the css will fire, but in case event.stopImmediatePropagation(), the next p click event will not fire.