Forms: event and method submit

Two pieces of the form story sit right next to each other and are easy to mix up. One is the submit event, which fires when a user tries to send a form. The other is the submit() method, which you call to send a form yourself. They point in opposite directions: the event is the browser telling your code “the user wants to submit,” and the method is your code telling the browser “send this now.”

The submit event is where validation lives. You catch it, look at what the user typed, and either let the form go through or stop it and handle things in JavaScript instead.

The submit() method goes the other way. You build or grab a form in code and fire it off to the server without waiting for a click.

User clicks / presses Enter
submit event
your handler validates
your code
form.submit()
server (no event)
The submit event flows from user to your handler; the submit() method flows from your code to the server.

Event: submit

A user can submit a form in two main ways:

  1. Click an <input type="submit"> or an <input type="image">.
  2. Press Enter while focused on an input field.

Both routes end at the same place: a submit event fires on the <form> element. That single entry point is what makes it convenient. You don’t need to wire up the button click and the Enter key separately; hook the form’s submit event once and you catch every path.

Inside the handler you inspect the data. If something’s wrong, show the error and call event.preventDefault(). That cancels the default action, so the form never leaves for the server and the page doesn’t reload.

Enter in a text field
click Submit button
↓ both ↓
submit event on <form>
data OK → form goes to server
preventDefault() → stays in JS
Two user actions, one submit event, one decision point.

In the form below, try both things: click into the text field and press Enter, then click the Submit button. Each one pops an alert, and the form goes nowhere because the handler returns false:

<form onsubmit="alert('caught it!');return false">
  First: focus this field and press Enter <input type="text" value="hello"><br>
  Second: click the button <input type="submit" value="Send">
</form>

Returning false from an inline onsubmit attribute is the old-school way to cancel the default action; it’s equivalent to calling event.preventDefault(). In a handler added with addEventListener, the return value is ignored, so there you must call preventDefault() explicitly.

Here’s that pattern working for real. The handler always calls preventDefault() so the page never navigates; then it decides whether the data is good enough to “send.” Submit it empty, then with a longer name, and watch the verdict change:

interactiveValidate on submit, then allow or cancel

Rather than an alert, this demo logs every event as it happens so you can see the order. Focus the field and press Enter — you’ll get a click on the button and a submit on the form. Then click the button directly and compare: the exact same two lines appear.

interactiveEnter fires a synthetic click, then submit

Method: submit

Sometimes you want to send a form from code, not from a user gesture. Call form.submit() and the browser submits it to the server as if it had been triggered normally.

There’s one important asymmetry: calling form.submit() does not fire the submit event. The reasoning is that if your script is the one submitting, your script has already done whatever checking it needed. The event exists to give you a chance to intervene on a user’s action, and here there’s no user action to intervene on.

user
submit event fires
server
code
form.submit(): no event
server
A user submit passes through the event; form.submit() bypasses it.

A common use is building a form on the fly and firing it off. This example creates a stock-lookup form in memory, points it at a URL, fills in a field, and submits:

let form = document.createElement('form');
form.action = 'https://quotes.example.org/lookup';
form.method = 'GET';

form.innerHTML = '<input name="ticker" value="ACME">';

// the form must be in the document to submit it
document.body.append(form);

form.submit();

Note the comment: the form has to be attached to the document before submit() will work. A form that only exists in a detached variable won’t submit. That’s why the code appends it to document.body first.

Summary lines to remember

  • The submit event fires on the <form> for both a submit-button click and Enter in a text field. Cancel it with event.preventDefault() (or return false in inline handlers) to keep the data in JavaScript.
  • Pressing Enter also produces a synthetic click on the form’s default submit button.
  • form.submit() sends the form from code and skips the submit event entirely; the form must be in the document first.
  • Watch for controls named after form properties like submit — they shadow the method.