Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch event that was stopPropagation

For example we have a page with a link that has onclick event listener on it. But handler makes stopPropagation. How I can handle that click event was made, if it's not bubble to root anymore?

e.g.

document.addEventListener('click', function(e) {console.log(e);});
a.onclick = function(e) {e.stopPropagation();};
like image 934
Alex Ivasyuv Avatar asked Feb 11 '15 12:02

Alex Ivasyuv


People also ask

What does event stopPropagation () do?

Event.stopPropagation() The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. It does not, however, prevent any default behaviors from occurring; for instance, clicks on links are still processed.

What does event stopPropagation () do ancestors?

The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.

What does event stopPropagation () do chegg?

Description: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.


1 Answers

DOM event propagation works in three phases: the capture phase, the target phase and the bubble phase. Roughly, the event first works its way down to the target, reaches the target and then works its way back up.

By default, event handlers are attached in the final bubble phase. If you attach a click event listener in the first capture phase, it will run before the previous handler has had the chance to call stopPropagation.

See this question for a deeper explanation.

like image 112
janfoeh Avatar answered Nov 02 '22 19:11

janfoeh