Event: preventDefault() method
The preventDefault() method cancels the event's default action.
Syntax
preventDefault()Parameters
None.
Return value
None (undefined).
Exceptions
None.
Description
- When
cancelableistrue, it setsdefaultPreventedtotrue, and thedispatchEvent()that dispatched it returnsfalse. - When
cancelableisfalse, it does nothing and doesn't throw (measured). - What the "default action" is depends on each event. For example, for
application'serrorevent, the default action is the runtime printing an error log.
Examples
js
application.addEventListener('ask', (e) => e.preventDefault());
console.log(application.dispatchEvent(new Event('ask', { cancelable: true })));
console.log(application.dispatchEvent(new Event('ask')));Result: Prints false and then true.