What does event.preventDefault() do?
Short answer
It cancels the browser's default action for that event — like following a link's href, submitting a form, or checking a checkbox — while still allowing the event to bubble unless you also stop propagation.
Advertisement
AModel answer
It cancels the browser's default action for that event — like following a link's href, submitting a form, or checking a checkbox — while still allowing the event to bubble unless you also stop propagation.
form.addEventListener('submit', (e) => {
e.preventDefault();
validateAndSubmitViaFetch();
});Real-world example
Intercepting a form submission to send data via fetch() instead of a full page reload.
Likely follow-ups
- Does preventDefault() also stop event bubbling? (No — that's stopPropagation().)
Related topics