How do I consume a key event in javascript, so that it doesn't propagate?
To stop an event from further propagation in the capturing and bubbling phases, you call the Event. stopPropation() method in the event handler. Note that the event. stopPropagation() method doesn't stop any default behaviors of the element e.g., link click, checkbox checked.
stopPropagation() The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
stopPropagation() will stop event from propagating to the window.
Use event. stopPropagation(). Show activity on this post.
If you don't want the event to propagate and you're not using jQuery (or another library that wraps native browser events), you need to use the event's stopPropagation()
method in most browsers and its cancelBubble
property in IE. Don't bother with return false
or preventDefault()
: those only affect whether the native browser action happens for the event and have nothing to do with propagation.
For example:
document.onkeypress = function(evt) {
evt = evt || window.event;
if (typeof evt.stopPropagation != "undefined") {
evt.stopPropagation();
} else {
evt.cancelBubble = true;
}
};
Try preventDefault
and/or stopPropagation
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With