0Pricing
JavaScript Academy · Lesson

The submit Event

Handle form submission and prevent reloads.

The submit Event is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “The submit Event” lesson free?

Yes — the full text of “The submit Event” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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