Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I handle a form submission without the page reloading?

Quick answer

Listen for the form’s submit event, then call event.preventDefault() inside the handler if your page will handle the submission itself. This cancels the browser’s default form action, which may otherwise navigate or submit in the browser’s usual way. Preventing that action does not provide your submission logic: add your own handling, or leave the default action in place. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What’s the basic way to stop a form from reloading the page when someone submits it?

Geez

Listen for the form’s submit event and call event.preventDefault() in that handler. That cancels the browser’s default submission behavior, so your page code can take over instead. [S1] Use it only when your page is actually handling the submission; cancellation alone does not provide your submission logic. [S2]

For example, assuming the page has a form with id="contact-form" and an element with id="status", a minimal handler could be:

const form = document.querySelector("#contact-form");
const status = document.querySelector("#status");

form.addEventListener("submit", (event) => {
  event.preventDefault();
  status.textContent = "Form submitted in this page.";
});

This example only updates a message; it does not send form data. Replace that line with the page’s actual submission handling.

Follow-up question

Why use the submit event instead of listening for a click on the button?

Geez

The form’s submit event is the event to handle when you want your code to respond to form submission. [S1] Attach the listener to the form itself, rather than relying on a click listener on one button. The example puts the submission logic in the form’s handler, where it can cancel the form’s default action when appropriate.

In the example, #contact-form is a placeholder selector: change it to match your form. If the selector does not find a form, the example will need to be adjusted to your page. Likewise, the status element is only there to demonstrate a visible response; you can replace it with the handling your page needs.

Follow-up question

Does preventDefault() send the form values somewhere or validate them?

Geez

No. preventDefault() cancels an event’s default action when that event is cancelable; it does not provide your own submission logic. [S2] In this pattern, canceling the form’s default action prevents the browser from carrying out its usual submission behavior. [S1] The handler must then decide what to do with the values. The example only displays a message—it does not send values or validate them.

For an actual submission, replace the example status update with your application’s handling. Avoid calling preventDefault() if you intend to rely on the browser’s usual form submission; otherwise, you have canceled that default behavior. [S1][S2] The appropriate way to handle or validate values depends on your application.

Follow-up question

How should I adapt and check the example before using it?

Geez

Treat the selectors and status text as placeholders. Match the form selector to your page, decide what your page should do with submitted values, and check that the resulting behavior matches your intention. If you want the browser’s normal form submission, leave out the cancellation and handle the form in the usual way. [S1]

A quick checklist:

  1. Confirm the selector matches the intended form.
  2. Attach the listener to that form’s submit event.
  3. Call preventDefault() only when page code will handle the submission.
  4. Replace the example message with your actual handling.
  5. Check that a submission produces the behavior you intend.

If the page should send data, this small example is not a complete sending implementation; add the submission handling your application requires.

A practical checklist

  • Select the form, not just a button.

  • Add a listener for the form’s submit event.

  • Call event.preventDefault() only when your page will handle the submission.

  • Add the actual page-specific handling for submitted values.

  • Check that the result matches whether you intend to handle the submission or use the browser’s default action.

A few more useful answers

Will `preventDefault()` stop every form submission?

It cancels an event’s default action when that event is cancelable. In this form pattern, it cancels the browser’s default submission behavior; it does not replace that behavior with your own. [S1][S2]

What should I put in the handler after calling `preventDefault()`?

Add the handling your page needs, such as displaying a message or using your application’s submission logic. The example only updates text; it does not send form data.

Can I leave out `preventDefault()`?

Yes. Leave it out if you want the browser’s default form submission behavior rather than taking over the submission in page code. [S1]

Sources

References used for this guide.

  1. [S1] HTMLFormElement: submit event — Web APIs | MDN
    developer.mozilla.org · Checked
  2. [S2] Event — Web APIs | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work