Home

PreventDefault

PreventDefault is a method on Event objects in the Document Object Model (DOM). It cancels the default action the browser would normally perform for the event. After calling it, the browser will not navigate to a link, submit a form, or perform other standard actions associated with the event.

Common uses include suppressing the default behavior of anchor tags, form submissions, and certain keyboard or

PreventDefault only stops the default action; it does not stop event propagation. To stop propagation through

Compatibility and alternatives: preventDefault is supported in all modern browsers. For very old environments, a common

See also: stopPropagation, stopImmediatePropagation, returnValue, Event interface.

mouse
events.
For
example,
in
an
event
handler
you
can
call
e.preventDefault()
to
stop
a
link
from
navigating,
while
allowing
your
own
logic
to
run.
It
is
also
used
to
implement
custom
form
validation
or
dynamic
user
interfaces
that
replace
default
browser
behavior.
the
DOM,
use
e.stopPropagation()
or
e.stopImmediatePropagation()
if
needed.
In
inline
event
handlers,
returning
false
can
in
some
environments
prevent
default
actions,
but
this
is
not
reliable
in
modern
standards,
so
explicit
calls
to
preventDefault
and
stopPropagation
are
preferred.
fallback
is
to
check
for
the
method
and
set
a
legacy
property,
such
as
if
(e.preventDefault)
e.preventDefault();
else
e.returnValue
=
false.