.submit()

Bind an event handler to the "submit" JavaScript event, or trigger that event on an element.

.submit(function(eventObjectEvent))🡢 jQuery

function(eventObjectEvent) FunctionA function to execute each time the event is triggered.

.submit(eventData, function(eventObjectEvent))🡢 jQuery

eventData AnythingAn object containing data that will be passed to the event handler.
function(eventObjectEvent) FunctionA function to execute each time the event is triggered.

.submit()🡢 jQuery

This method is a shortcut for .on( "submit", handler ) in the first variation, and .trigger( "submit" ) in the third.

The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to <form> elements. Forms can be submitted either by clicking an explicit <input type="submit">, <input type="image">, or <button type="submit">, or by pressing Enter when certain form elements have focus.

Depending on the browser, the Enter key may only cause a form submission if the form has exactly one text field, or only when there is a submit button present. The interface should not rely on a particular behavior for this key unless the issue is forced by observing the keypress event for presses of the Enter key.

For example, consider the HTML:

<form id="target" action="destination.html">
  <input type="text" value="Hello there" />
  <input type="submit" value="Go" />
</form>
<div id="other">Trigger the handler</div>

The event handler can be bound to the form:

$("#target").submit(function (event) {
  alert("Handler for .submit() called.");
  event.preventDefault();
});

Now when the form is submitted, the message is alerted. This happens prior to the actual submission, so we can cancel the submit action by calling .preventDefault() on the event object or by returning false from our handler. We can trigger the event manually when another element is clicked:

$("#other").click(function () {
  $("#target").submit();
});

After this code executes, clicks on Trigger the handler will also display the message. In addition, the default submit action on the form will be fired, so the form will be submitted.

The JavaScript submit event does not bubble in Internet Explorer. However, scripts that rely on event delegation with the submit event will work consistently across browsers as of jQuery 1.4, which has normalized the event's behavior.

If you'd like to prevent forms from being submitted unless a flag variable is set, try:

JS
<p>Type 'correct' to validate.</p>
<form action="javascript:alert( 'success!' );">
  <div>
    <input type="text" />
    <input type="submit" />
  </div>
</form>
<span></span>
CSS
p {
  margin: 0;
  color: blue;
}
div,
p {
  margin-left: 10px;
}
span {
  color: red;
}
HTML
$("form").submit(function (event) {
  if ($("input").first().val() === "correct") {
    $("span").text("Validated...").show();
    return;
  }

  $("span").text("Not valid!").show().fadeOut(1000);
  event.preventDefault();
});
DEMO

If you'd like to prevent forms from being submitted unless a flag variable is set, try:

HTML
$("form").submit(function () {
  return this.some_flag_variable;
});
DEMO

To trigger the submit event on the first form on the page, try:

HTML
$("form").first().submit();
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)