Back to results for “XHR”
JavaScript › DOM & Events

What does event.preventDefault() do?

Beginner

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.

Common follow-ups: Does preventDefault() also stop event bubbling? (No — that's stopPropagation().)