Please tell me why the e.stopPropagation()
function does not work?
$(document).ready(function() {
var el = '<div class="el" onclick="alert(1);"><div class="stop">X</div>Click</div>';
$("body").append(el);
})
$(document).on("click", ".stop", function(e) {
e.stopPropagation();
e.preventDefault();
alert(2);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
</body>
2
Answers
It does work. Observe:
In your code the event propagation is indeed being stopped. However, there are no further event handlers up the DOM tree so there’s no propagation to observe in the first place. What you have are two separate
click
handling events on the same element, both of which are being processed independently.In the example shown here, a parent element (
div.outer
) does have an event handler, and the propagation stops before it reaches that handler. Without stopping propagation, you’d see that event handler invoked:You might be looking for
stopImmediatePropagation()
instead? This will prevent further event handlers from being invoked on the same element. For example:In this case The second
click
handler on the target element stopped furtherclick
handlers on that same element from being processed. Note that this has no effect on the firstclick
handler, which was already invoked by then.Edit: Your updated code demonstrates a slightly different problem. (Which was being obscured in the original code by the original problem addressed above.)
Notice how you are attaching your jQuery event handler. Which DOM object are you attaching it to?…
document
. Which is much further up the DOM hierarchy and isn’t reached until after the parent element has already processed its inlineclick
handler.If you attach a
click
handler directly to the element, you can see the difference:Another edit for the ongoing moving target…
You’re still essentially running into the same issue. One click handler is on the element itself, another is on the
document
object. The latter won’t be processed before the former.Appending the elements to the DOM after the fact may indeed complicate things a little in this case, and you may need to attach the click handler to the element(s) themselves after appending them. For example, this stops the propagation:
The key difference here is that the jQuery
click
handler isn’t being attached to thedocument
or relying on event delegation/propagation in the first place. It’s being attached directly to the target element, and as such needed to be attached after the element is added to the DOM.First, you made a typo.
class
only has 2s
s in it.That aside, the event starts on the div that you click. It then propagates to the div with the
onclick
attribute (triggering that), then continues up the DOM until it hits thedocument
object and triggers the event handler bound withjQuery
at which pointstopPropagation
is called and it stops.If you want to prevent the
onclick
attribute triggering then you need to handle the event in the capture phase. I don’t think jQuery supports that so you’ll need to use native DOM instead.