'How check if custom event was prevented
In my code I need to interact with custom event after dispatched.
// ...
let customEvent = new CustomEvent('myevent', {
bubbles: true,
cancelable: true
});
button.addEventListener('click', function (e) {
e.preventDefault();
otherElement.dispatchEvent( customEvent );
// at this point i neeed some help
// if ( customEvent was canceled or prevented ) { do something }
});
How can check if it has been prevented/cancelled or another solution to get some "feedback" from event?
Solution 1:[1]
You can check for the Boolean defaultPrevented in the event Object
Solution 2:[2]
You can check for the defaultPrevented property. Also the dispatchEvent method returns whether the default action is still to take place or not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | epascarello |
| Solution 2 | Bergi |
