The preventDefault() method of the Event interface tells the user agent that the event is being explicitly handled, so its default action, such as page scrolling, link navigation, or pasting text, should not be taken.
The event continues to propagate as usual,
unless one of its event listeners calls
stopPropagation()
or stopImmediatePropagation(),
either of which terminates propagation at once.
As noted below, calling preventDefault() for a
non-cancelable event, such as one dispatched via
EventTarget.dispatchEvent(), without specifying
cancelable: true has no effect.
If a passive listener calls preventDefault(), nothing will happen and a console warning may be generated.
Note:
Look for better alternatives than using preventDefault() to block default actions. For example, you can use the disabled or readonly attribute on a form control to prevent it from being interacted with, use HTML constraint validation to reject invalid input, or use the overflow property to prevent scrolling.