jQuery API

.submit()

.submit( handler(eventObject) ) Returns: jQuery

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

  • version added: 1.0.submit( handler(eventObject) )

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.4.3.submit( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.submit()

This method is a shortcut for .bind('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() {
  alert('Handler for .submit() called.');
  return false;
});

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.

Additional Notes:

  • Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.

Examples:

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

<!DOCTYPE html>
<html>
<head>
  <style>

  p { margin:0; color:blue; }
  div,p { margin-left:10px; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Type 'correct' to validate.</p>
  <form action="javascript:alert('success!');">
    <div>
      <input type="text" />

      <input type="submit" />
    </div>
  </form>
  <span></span>
<script>

    $("form").submit(function() {
      if ($("input:first").val() == "correct") {
        $("span").text("Validated...").show();
        return true;
      }
      $("span").text("Not valid!").show().fadeOut(1000);
      return false;
    });
</script>

</body>
</html>

Demo:

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

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

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

$("form:first").submit();

Support and Contributions

Need help with .submit() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .submit()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Anonymous

    @scotty I too was failing silently with … changed the ‘id’ and ‘name’, and works fine now.

  • Anonymous

    If you don’t know the form id, but you know the elements you can grab the form then hook it’s submit function to do some additional processing:
    $(document.getElementById(‘elementId’).form).submit(function() {
    // form processing
    return true;
    });

  • Ian

    Why doesn’t this event handler work with multipart forms?

  • http://twitter.com/HighwayofLife David Lewis

    You can stop the form from being submitted with either return false or preventDefault(), however, it should be noted that if you have a JavaScript error in your function, the form will submit regardless.

  • http://circlesoftware.com.au/ Ian

    Ok, got it working.

    It was hard to get my head around it working with the DOM and then what has been posted / reset and if the event handler could find the object in the DOM.

    Basically, I put this event handler on a link but also included a submit input type in the form as well. (some browsers wont submit the form without one).

    Now you don’t want the submit button and the link to show at the same time. So what I have done is just simply hide the submit button with CSS.

    Now I can bind any event on the link and then submit the form, for example I can place an uploading message in the DOM and then submit the form so users know that they need to wait a little while.

    Cheers.

  • http://momentumworkshop.com Corey Frang

    An alternate workaround exists (if you can’t rename it):

    $form.find(‘input[name=submit]‘).click();

    Something really should be added to the source to detect this strange case…

  • balpha

    Note that in Firefox, .submit() will (silently) fail if the the form has not been inserted into the DOM:

    $(“<form action='/' />”).submit()

    works (at least) in Chrome and Opera, while it fails (at least) in FF. So be sure to make it

    $(“<form action='/' />”).appendTo($(“body”)).submit()

  • John Paul

    “default submit action on the form will be fired, so the form will be submitted”

    Is this statement in the above documentation true? If there is a submit handler returns false, won't it still not submit? When testing it myself in firefox, the form does not submit.

  • John Paul

    “default submit action on the form will be fired, so the form will be submitted”

    Is this statement in the above documentation true? If there is a submit handler returns false, won't it still not submit? When testing it myself in firefox, the form does not submit.

  • Pieter

    I noticed the submit() event will fail silently in Firefox (whereas it works in Chrome, and possibly others) when no <input type=”submit”> exists. A button will not be sufficient.

  • Pieter

    I noticed the submit() event will fail silently in Firefox (whereas it works in Chrome, and possibly others) when no <input type=”submit”> exists. A button will not be sufficient.

  • http://www.learningjquery.com/ Karl Swedberg

    A button will work fine if you also give it type=”submit”

  • http://www.learningjquery.com/ Karl Swedberg

    A button will work fine if you also give it type=”submit”

  • mark_nsx

    how do you get the input that triggered the submit handler? cheers!

  • mark_nsx

    how do you get the input that triggered the submit handler? cheers!

  • http://twitter.com/quickredfox quickredfox

    ” scripts that rely on event delegation with the submit event will work consistently across browsers as of jQuery 1.4″

    IMHO

    ” scripts that rely on event delegation with the submit event SHOULD work consistently across browsers as of jQuery 1.4″

    Because it dont.

  • http://twitter.com/quickredfox quickredfox

    ” scripts that rely on event delegation with the submit event will work consistently across browsers as of jQuery 1.4″

    IMHO

    ” scripts that rely on event delegation with the submit event SHOULD work consistently across browsers as of jQuery 1.4″

    Because it dont.

  • jspies

    I believe you have to use click events on the inputs themselves and then you can call submit() to submit the form or pass it to the default.

    It'd be nice if relatedTarget contained it.

  • jspies

    I believe you have to use click events on the inputs themselves and then you can call submit() to submit the form or pass it to the default.

    It'd be nice if relatedTarget contained it.

  • An0n1m0us

    I've used a <button type=”submit”> and .submit() seems to fail silently. I'm very confused. Am using jQuery 1.3.2 at the moment. I do not understand how the event not bubbling in IE would make any difference to my current case since I've not tested in IE yet. Just trying to get Firefox working. Why does this fail to submit the form using 1.3.2 and Firefox 3.6.6?

    $('#declare_use').submit(

    function (event_ref) {

    event_ref.preventDefault();

    if (verify() && all_checked_ok()) {

    this.submit();

    } else {

    user_feedback();

    }

    }

    );</button>

  • An0n1m0us

    I've used a <button type=”submit”> and .submit() seems to fail silently. I'm very confused. Am using jQuery 1.3.2 at the moment. I do not understand how the event not bubbling in IE would make any difference to my current case since I've not tested in IE yet. Just trying to get Firefox working. Why does this fail to submit the form using 1.3.2 and Firefox 3.6.6?

    $('#declare_use').submit(

    function (event_ref) {

    event_ref.preventDefault();

    if (verify() && all_checked_ok()) {

    this.submit();

    } else {

    user_feedback();

    }

    }

    );</button>

  • http://twitter.com/psynnott Paul Synnott

    If you're wondering why your submit handler isn't taking effect, don't be like me and spend 45 minutes debugging it only to find out you didn't place the .submit() bind in $(document).ready.

  • http://twitter.com/psynnott Paul Synnott

    If you're wondering why your submit handler isn't taking effect, don't be like me and spend 45 minutes debugging it only to find out you didn't place the .submit() bind in $(document).ready.

  • Clarkzheng

    Hi, i wonder whether there is a API like .getJSON(url,callback) to parsing the result of submitted form with JSON format for API .submit. Because in my form , user could upload one file as a attachment for program to parse by JAVA. In such case, i cannot use .getJSON(url,callback) since it contains a complex file-type parameter. thx

  • Clarkzheng

    Hi, i wonder whether there is a API like .getJSON(url,callback) to parsing the result of submitted form with JSON format for API .submit. Because in my form , user could upload one file as a attachment for program to parse by JAVA. In such case, i cannot use .getJSON(url,callback) since it contains a complex file-type parameter. thx

  • Clarkzheng

    I got one solution. One plug-in named as JQuery-ajaxform could meet my requirement. Just wonder whether there is any API like in JQuery source code? Hope such function could be added in further version.

  • Clarkzheng

    I got one solution. One plug-in named as JQuery-ajaxform could meet my requirement. Just wonder whether there is any API like in JQuery source code? Hope such function could be added in further version.

  • Clarkzheng

    I got one solution. One plug-in named as JQuery-ajaxform could meet my requirement. Just wonder whether there is any API like in JQuery source code? Hope such function could be added in further version.

  • zwacks

    Thanks a lot, it really works when you remove/change the name and id.

  • zwacks

    Thanks a lot, it really works when you remove/change the name and id.

  • zwacks

    Thanks a lot, it really works when you remove/change the name and id.

  • http://blog.markstahler.ca/ Mark

    This needs to be stickied or in the documentation.

  • http://blog.markstahler.ca/ Mark

    This needs to be stickied or in the documentation.

  • http://blog.markstahler.ca/ Mark

    This needs to be stickied or in the documentation.

  • Choco_late

    Thanks a lot for that!! very helpful!!

  • Choco_late

    Thanks a lot for that!! very helpful!!

  • Manishk Sharma

    return false job is stop the default action of what ever you are doing…no wonder why the form does not submit when u tested it. but if there would have been any JS error in you code..the form would have been submitted regardless of return false coz return false could not work as there was a jS error..

  • http://chrisblunt.com/ Chris Blunt

    This was causing me some problems. Thanks for pointing it out. Should definitely be noted in the documentation.

  • http://misbehavens.myopenid.com/ Andrew

    Also look at this ajax file upload solution: http://valums.com/ajax-upload/. I have used it and have been very happy. It uses only javascript, taking advantage of newer browser technologies, but also degrades well to work with older browsers (like IE 6)

  • Guest

    That is the stupidest thing i have ever heard. Why would you create an element with and id/name that is a reserved word? All ids/names should always be completely unique and have a more relevant name than 'submit'. I should be able to look at the name and see what is does or what it is. I have never been in a senario where i could not change the name. This prevents so many dilemas.

  • Guest

    it is possible to retrieve the submitted button in the .submit() function? for example if I have 2 submit buttons, may I retrive the name of the pushed buttons?

  • hureaerocvry

    GOOOD

  • Hacklessy

    GAME APA INI???

  • Hacklessy

    GAME APA INI???

  • Latibro

    As .submit() is an event, you will be able to get the event object which will tell you which DOM object has triggered the event.

  • Ade

    I'm trying to check som data using this code:

    $(“form#574_0_163_5″).submit(function() {jVal.submitcheck(this); return false;});

    Why isn't it working? I'm using jquery 1.4.

  • Okaypeace

    Maybe you should try $(“#574_0_163_5″)

  • Robert D.

    $(“[type=submit],[type=image]“).bind(“click”, function(e){
    $(this).attr(“was_clicked_to_submit”,”YES”);
    });

    $(“form”).bind(“submit”,function(event){

    var theElementThatTriggeredTheSubmit = $(“[was_clicked_to_submit=YES]“).get(0);

    });

  • Okaypeace

    or this might work as well:

    function getTarget(e){
    var target;
    if (!e) var e = window.event;
    if (e.target) target = e.target;
    else if (e.srcElement) target = e.srcElement;
    if (target.nodeType == 3) // defeat Safari bug
    target = target.parentNode;
    return target;
    }

    $(“form”).submit(function(event){
    var theElement = getTarget(event);
    // but i think theElement will be the actual form – haven't tested it – i'm saying this because the element that has the event submit assigned to is the form and not the submit button. So i would go with the solution posted previously
    });

  • Azania

    Does not work, both event.target and event.currentTarget refer to the form and event.relatedTarget is undefinded. It will keep saying the form has triggered the event.

  • Sadovnikoff

    how can i make submit into a new window just like acting “target”-property for anchors?

  • gmkv

    If you're tearing your hair over why submit() just isn't submitting a form, make sure that the submit input tag does not have name=”submit” as an attribute, or jQuery won't submit the form.

  • Pedromagnus

    Two days losted (or wasted) because I could never possibly knew that a submit event (for example) could have multiple handlers.
    How could be possible to NOT mention none of it in any place at all in this doc.
    I like jQuery but this is really dissapointing.

  • sW`

    I've got img tag with id=”submit” and it reproduced the same problem.

  • Fake

    Yeah, they should probably put that in the documentation.

  • Abdul Gaffar

    Just checking the ajax form

  • jmc

    duh! bacon saved. thanks.

  • Andreas

    Indeed. If you remove name=”submit”, the submit handler is called.
    Is there any explanation for this behavior?

  • Fr34k

    Is it possible, to make a Form like this:
    <form action=”javascript:” method=”POST”>
    <input name=”text” type=”text”>
    <input name=”submit” type=”submit”>

    Then i will make a JQuery, which send the form. After that I will print the field 'text' per php out?
    How can I do that?

    Greetz</form>

  • Fr34k

    Is it possible, to make a Form like this:
    <form action=”javascript:” method=”POST”>
    <input name=”text” type=”text”>
    <input name=”submit” type=”submit”>

    Then i will make a JQuery, which send the form. After that I will print the field 'text' per php out?
    How can I do that?

    Greetz</form>

  • Rahul

    Thanks GMKV.. You saved my day.. I was really going frustrated.. your solution works as a charm

  • Irv

    can you use .live() with the event .submit()

  • corlando

    Does anyone know how I can load the results of a PHP “quiz” (a form) into a MODAL/LIGHTBOX popup – which should OPEN AS SOON AS THE SUBMIT BUTTON IS CLICKED? Been looking for this all night and still cannot find a solution, they all require a LINK not a SUBMIT button.

  • Phil Stowell

    Thank you for this solution also, was just starting to get stressed out.

  • Irv

    I have already found out the answer to this question.

    In 1.4 and above you can.

  • http://staffpoll.com StaffPoll

    For some reasons when my form has method=”post” and I call submit() then method changed to get, anybody have same problem?

  • newbee

    Hi, i wonder if there is any example to the function .submit( [ eventData ], handler(eventObject) )? thanks

  • peg

    Hi all,
    I have a

    $(class-selector).live('submit', function(){ ajax call })

    event handler and a
    a.href click handler that submits the related form …

    that was wortking and since ? I have 4 posts of the same form !
    how can I control/verify the number of same event handling or the creation of multiple events ?
    thanks