function h(e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
alert(e.type);
return false;
}
document.querySelector('.wrapper').addEventListener('mouseup', h, false);
document.querySelector('.child').addEventListener('click', h, false);
<div class='wrapper'>
<button class='child'>Click me</button>
</div>
I expect this to prevent the 'click' event from firing, but it doesn't. However, changing mouseup
to mousedown
does in fact prevent the click event.
I've also tried setting the useCapture
argument to true, and that also doesn't produce the desired behavior with mouseup
. I've tested this on Chrome and Firefox. Before I file bugs, I figured I'd ask here.
Is this a bug in current browsers, or is it documented behavior?
I've reviewed the W3C standard (DOM level 2), and I wasn't able to find anything that could explain this behavior, but I could have missed something.
In my particular case, I'm trying to decouple two pieces of code that listen to events on the same element, and I figured using capture events on the part that has priority would be the most elegant way to solve this, but then I ran into this problem. FWIW, I only have to support officially supported versions of FF and Chrome (includes ESR for FF).
preventDefault
andstopImmediatePropagation
are unnecessary here (by the way), because there's no<form>
so no submitting with the button, and no multiple events of the same type. And I think this is expected behavior because doesn'tmouseup
fire beforeclick
? So the.child
'smouseup
event fires (and bubbles) before itsclick
event (which you have stopped, but it doesn't matter) – ScrotumuseCapture
to true forces.wrapper
's mouseup to happen before the bubble from child. I didn't include this code, because in either case, I should only get one alert() (with a different originating element), right? – Wages