Event bubbling in JS
Hello guys, sorry to disturb you all; in order to stop an event from propagating to its parent element, we can use the following code:
My question is, does the order in which we write the line e.stopPropagation(); matters please
7 Replies
no, it can be anywhere as long as it's executed before the end of the codeblock
tho i would say putting at the beginning makes more sense. that's more intuitive imo
more readable too
yep got it, thanks !
(the main use for not having it at the top would be a conditional bubble stop, btw. Either based on general app state, or on the actual element being targeted not meeting certain criteria where the parent might)
ahh yeah I see, like we can execute the stopPropagation based on some kind of conditions
You can use checking which of the elements was clicked, sometimes a useful option