15

I'm using event.stopPropagation() in my application. There has appeared, however, one scenario where I want the propagation to continue as if the aforementioned function was never called. So I'm wondering; is there a way to "resume" propagation after it's been stopped? It would be terribly tedious to move the one call to event.stopPropagation to a dozen separate conditional statements.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • Mmmm...http://stackoverflow.com/questions/4735006/javascript-how-to-enable-stoppropagation – elclanrs Feb 17 '12 at 08:20
  • If there were such a facility, wouldn't you need a dozen conditional statements to decide if you should invoke it? – Jon Feb 17 '12 at 08:22
  • They are already there. There would be one call to `event.stopPropagation()` and (rarely) one call to the hypothetical `event.resumePropagation()`. That would be preferable over calling `event.stopPropagation()` in all conditionals except one – Hubro Feb 17 '12 at 08:29

4 Answers4

16

Once propagation has been stopped, it cannot be resumed. As a workaround, what you can do is set a variable, and then only stop if this variable is true:

var stop = false;
// do your logic here
if(stop){
    event.stopPropagation();
}
Schiavini
  • 2,869
  • 2
  • 23
  • 49
  • 3
    I didn't think of that. That's way cleaner than my backup solution. It would be optimal if you could edit your answer and include the fact that "once propagation has been stopped, it **can't** be resumed" – Hubro Feb 17 '12 at 09:45
2

Just refactor your original event like this:

var refEvent = event.originalEvent;
refEvent.cancelBubble = false;
refEvent.defaultPrevented = false;
refEvent.returnValue = true;
refEvent.timeStamp = (new Date()).getTime();

if (event.target.dispatchEvent){
    event.target.dispatchEvent(refEvent);
} else if (event.target.fireEvent) {
    event.target.fireEvent(refEvent);
}
Jeffrey Rosselle
  • 745
  • 1
  • 9
  • 25
  • 4
    Can you provide a working example of an event being stopped with e.stopPropagation() and then restarted with your example above? I couldn't get it to work, but it looks right in theory. – gfullam Mar 20 '15 at 17:29
2

Here is a solution that works:

$('#mydiv').on('click.new', function(e){

    e.stopImmediatePropagation();

alert('this will happen only once');

$('#mydiv').unbind('click.new');

});

It makes use of the fact that event can have custom namespace, and can unbind accordingly. It also works for e.stopPropagation. For that matter, it can undo anything associated with that custom click event.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Jsproject
  • 31
  • 3
2

Put the event.stopPropagation() call inside your condition. For example

$el.click(function(event) {
    if (some_condition) {
        event.stopPropagation()
        // do stuff
    }
    else {
        // do other stuff, without stopping propagation
    }
});

Tedious it may be, but unfortunately stopPropagation is a one way switch. Once stopped, you can't turn it back on for the same event.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • It's not "a" condition. It's a **lot** of conditions. Quoting myself: "It would be terribly tedious to move the one call to event.stopPropagation to a dozen separate conditional statements" – Hubro Feb 17 '12 at 08:24
  • Tedious it may be, but unfortunately `stopPropagation` is a one way switch. Once stopped, you can't turn it back on for the same event. – Rory McCrossan Feb 17 '12 at 08:28
  • 1
    Now **that** is the answer I was looking for. As that actually answers my question, I will accept it if you edit – Hubro Feb 17 '12 at 08:31