.submit()S2C Home « Events « .submit()

Submit event handler.

Description

The .submit() method is used to bind an event handler to the JavaScript submit event or trigger that event on the specified element.

  • The submit event is sent to an element when the user is attempting to submit a form and can only be attached to form elements. Forms can be submitted by:
    1. Clicking a <button type=submit> element.
    2. Clicking a <input type=image> element.
    3. Clicking a <input type=submit> element.
    4. Pressing the 'Enter' key when certain form elements have focus. Dependant upon the browser, pressing the 'Enter' key may only submit the form if there is exactly one text field, or in other cases only if the form has a 'submit' button coded. Because of this do not rely on a particular behavior for this key, unless observing the .keypress() event for the 'Enter' key.

Syntax


Signature Description
.submit( )Trigger the submit JavaScript event on the specified element.
.submit( [eventData], handler(eventObject) )Bind an event handler to the submit JavaScript event, optionally passing aAn object of data.

Parameters


Parameter Description Type
eventDataAn object of data to pass to the event handler.Object
handler( eventObject )A function to execute each time the event is triggered.Function

Return

A jQuery object.

.submit( ) Examplego to top of page Top

Trigger the submit JavaScript event on the specified element.

  • This signature is a shortcut for .trigger('submit').

In the example below we show a new message in the 'div' element with an id of 'div9' when the user clicks the 'submit' button of the form, presses the 'Enter' key or clicks anywhere in the input.

When the input is clicked on we trigger off the submit JavaScript event on the 'ourform' class. This then fires off the $('#div9').submit(function(){}) code which outputs the message. Clicking the 'submit' button of the form or pressing the 'Enter' key may also trigger the event, see the information about this in the description above.

Pie Survey

     

Which pie do you prefer?:

            



$(function(){
  $('.ourform').submit(function () { 
    $('#div9').append('<code>submit</code> JavaScript event triggered<br />');
    return false; // disable submit 
  });
  $('#div9').click(function() {
    $('.ourform').submit();
  });
});

div9. Some initial text.



.submit( [eventData], handler(eventObject) ) Examplego to top of page Top


  • This signature is a shortcut for .bind('submit', handler).

Bind an event handler to the submit JavaScript event, optionally passing aAn object of data.

In the example below we show a new message in the 'p' element with an id of 'scrollspan5' each time the 'submit' button in the form below is clicked or the form has focus and the 'Enter' key is pressed.

When the 'submit' button in the form below is clicked or the form has focus and the 'Enter' key is pressed, the submit JavaScript event fires off the $('.ourform2').submit({ param1: '#scrollspan5', param2: 'submit ', param3: '**JavaScript event triggered** ' }, addText2); code.

What we are doing here is passing across the event object to the function addText3(event). The map we specify, in our case { param1: '#scrollspan5', param2: 'submit ', param3: '**JavaScript event triggered** ' } gets tagged onto the event.data property. We then access this parameter in the function via event.data.param and use it as part of the appended data.

Which pie do you prefer?:

            



$(function(){
  $('.ourform2').submit({ param1: '#scrollspan5', param2: 'submit ', 
                         param3: '**JavaScript event triggered**  ' }, addText3);
    return false; // disable submit 
  });
  function addText3(event) {
    $(event.data.param1).append(event.data.param2 + ''<code>' + event.data.param3+ ''</code>');
    return false;
  }
});

We will show a message here.

go to home page Homepage go to top of page Top