JavaScript Academy · Lesson

The submit Event

Handle form submission and prevent reloads.

Lesson 2 of 413 steps

The submit Event is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

Submitting Forms

The submit event fires when a form is sent, whether by clicking a submit button or pressing Enter in a field. It is the right place to handle form data.

Listening on the Form

Attach the listener to the form element itself, not to the submit button, so all submission paths are covered.

const form = document.querySelector("#signup");
form.addEventListener("submit", (e) => {
  console.log("submitting");
});

The Default Reload

By default, submitting navigates or reloads the page. For JavaScript-driven apps you usually cancel this.

form.addEventListener("submit", (e) => {
  e.preventDefault();
});

Why preventDefault Here

Calling preventDefault keeps the page in place so you can validate and send data yourself, often via fetch.

form.addEventListener("submit", (e) => {
  e.preventDefault();
  const data = new FormData(form);
  // send data without reloading
});

Submit Buttons

A button with type="submit" (the default inside a form) triggers submission. Use type="button" for buttons that should not submit.

Enter Key Submits

Pressing Enter in a single-line text field submits the form, which is why listening on the form is essential.

requestSubmit

form.requestSubmit submits programmatically while still firing the submit event and running validation, unlike form.submit which skips both.

form.requestSubmit(); // fires submit event + validates
// form.submit(); // bypasses validation and the event

Reading Data on Submit

Inside the handler, build a FormData from the form to gather all named fields cleanly.

form.addEventListener("submit", (e) => {
  e.preventDefault();
  const fd = new FormData(form);
  const obj = Object.fromEntries(fd.entries());
  console.log(obj);
});

Disabling the Button

To prevent double submission, disable the submit button while processing, then re-enable it when done.

form.addEventListener("submit", (e) => {
  e.preventDefault();
  const btn = form.querySelector("[type=submit]");
  btn.disabled = true;
});

Resetting a Form

form.reset clears fields back to their default values, and the reset event lets you respond.

form.addEventListener("reset", () => {
  console.log("cleared");
});

Submitter Element

The submit event’s submitter property tells which button triggered it, useful when a form has multiple submit buttons.

form.addEventListener("submit", (e) => {
  console.log(e.submitter && e.submitter.value);
});

Quick Check

Choose the best practice.

Recap

Handle submissions with the form’s submit event, calling preventDefault for JS-driven apps. Listen on the form (not the button) to catch Enter-key submits. Use FormData to read fields, requestSubmit to submit programmatically with validation, and disable the button to avoid double submits.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “The submit Event” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “The submit Event”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “The submit Event”?

Handle form submission and prevent reloads. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “The submit Event” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading Input Values
  2. The submit Event
  3. Client-Side Validation
  4. The Constraint Validation API
← Back to JavaScript Academy