How to Handle Form Submit in JavaScript
Use form submit handlers when you need to validate input, prevent page reloads, send data to an API, or show success states after a user submits. The cleanest pattern is listening for the form’s "submit" event.
What you’ll build or solve
You’ll learn how to handle form submit in JavaScript using addEventListener("submit", ...). You’ll also know how to prevent default reload behavior and safely read field values.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when form data needs custom logic before or after submission.
Common real-world scenarios include:
- Login forms
- Newsletter signup
- Checkout validation
- Search filters
- API form submission
This is a bad idea when relying on inline onsubmit HTML attributes. Event listeners are easier to scale and maintain.
Prerequisites
You only need:
- A basic HTML form
- A JavaScript file or
<script>tag - Basic DOM selection knowledge
Step-by-step instructions
Step 1: Select the form and listen for "submit"
Start by selecting the form element.
HTML
<form class="signup-form">
<input class="email" type="email">
<button type="submit">Join</button>
</form>
<script>
const form = document.querySelector(".signup-form");
form.addEventListener("submit", (event) => {
event.preventDefault();
console.log("Form submitted");
});
</script>
Use the event callback to read field values.
JavaScript
const email = document.querySelector(".email").value;
console.log(email);
This pattern is ideal for validation and API requests.
What to look for:
- Listen on the
<form>, not just the button - Use
"submit"as the event type preventDefault()stops page reload- Read field values inside the callback
- Great for validation and API calls
Examples you can copy
Login form
JavaScript
loginForm.addEventListener("submit", (event) => {
event.preventDefault();
});
Newsletter signup
JavaScript
newsletter.addEventListener("submit", (event) => {
event.preventDefault();
console.log(emailInput.value);
});
Search form
JavaScript
searchForm.addEventListener("submit", (event) => {
event.preventDefault();
runSearch();
});
Common mistakes and how to fix them
Mistake 1: Listening on the button instead of the form
What the reader might do:
JavaScript
button.addEventListener("submit", () => {});
Why it breaks: submit events belong to forms.
Corrected approach:
JavaScript
form.addEventListener("submit", () => {});
Mistake 2: Forgetting preventDefault()
What the reader might do:
JavaScript
form.addEventListener("submit", () => {
console.log("Submitted");
});
Why it breaks: the page reloads immediately.
Corrected approach:
JavaScript
form.addEventListener("submit", (event) => {
event.preventDefault();
});
Mistake 3: Reading values outside the submit callback
What the reader might do:
JavaScript
const email = emailInput.value;
Why it breaks: the value may be stale before the user submits.
Corrected approach:
Read values inside the submit handler.
JavaScript
form.addEventListener("submit", () => {
console.log(emailInput.value);
});
Troubleshooting
If the page reloads, confirm preventDefault() runs.
If nothing happens, verify the listener is attached to the form.
If values seem outdated, read them inside the submit callback.
If pressing Enter does not trigger logic, confirm the button uses type="submit".
Quick recap
- Listen for
"submit"on the form - Use
preventDefault() - Read values inside the callback
- Great for validation and API requests
- Prefer listeners over inline
onsubmit
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot