Skip to content

Form Submit Default Not Prevented

Quick summary

You handled submit but forgot e.preventDefault() — the browser performs a full reload and your JS handler's work is lost.

Why this happens

  • Missing e.preventDefault() in the submit handler

  • Button type defaults to submit inside a form, triggering reload

  • Handler returns true (or nothing) allowing the default action

Minimal example

✗ Broken code
form.addEventListener('submit', e => {
  saveData(); // page reloads — state lost
});
✓ Fixed code
form.addEventListener('submit', e => {
  e.preventDefault();
  saveData(); // no reload, state preserved
});

Without preventDefault, the browser reloads the page on submit, losing SPA state. Calling e.preventDefault() keeps navigation in your handler.

How to diagnose

  • Observe a full page reload after submit — that means default was not prevented

  • Check the submit handler for preventDefault call

  • Verify the button type is not accidentally submit

How to fix

  • Call e.preventDefault() at the start of the submit handler

  • Set button type="button" if it should not submit the form

  • Return false from legacy handlers to prevent the default

How to prevent

  • Always preventDefault in SPA form submit handlers

  • Use type="button" for buttons that trigger JS only

Related Resources

Related Glossary

Related Lessons

  • DOM

    Learn about form events.

← Back to language