How to stop event propagation of parent element if you don't have access to its code?

930 Views Asked by At

If I append a child element to a parent element, is it possible to stop the propagation of the parent's event to the appended child if I don't have access to the parent element's event listener code? If it's even possible, how?

Is there a way to do this inside the child event listener? Is there something like e.target.parentElement.stopPropagation()?

1

There are 1 best solutions below

0
On

If the event passes through the child you appended, you can stop it by calling stopPropagation on it. It has nothing to do with what element's handler does that; all that matters is that one of them calls that method on the event. So just e.stopPropagation().

If the event doesn't pass through the child (it's directly targeted at the parent, or passes through some other child to the parent), you can't stop propagation of it unless you can attach your handler to the parent element before the other handler is attached. But from your description, it doesn't sound like you can do that.