2

I want the form's onsubmit handler to be called,but don't want to submit the form.

Is this possible with jQuery?

yoyo
  • 1,113
  • 2
  • 17
  • 25

2 Answers2

5

Yes it is. I would recommend stopping the default action of the event from being triggered, like this:

$("#myForm").submit(function(e) {
    e.preventDefault();
});

See http://api.jquery.com/event.preventDefault/

You can also use return false at the end of the event handler. These two methods, however, are not exactly equivalent, so it is important to understand their differences.

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406
  • @yoyo - yes, correct. Please read the link, as well as the events guide: http://docs.jquery.com/Events_(Guide) – karim79 Dec 28 '10 at 06:10
  • But I don't want it that way,I need it to be submitted when certain condition is met... – yoyo Dec 28 '10 at 06:17
  • So first of all mention that in the Q :) Second of all, you can 'return true' or `$(this).submit()` when a certain condition is met, which will trigger the submit. – karim79 Dec 28 '10 at 06:29
3

Try something like:

$('#yourForm').trigger('submit');

Read this trigger and triggerHandler

Fidi
  • 5,754
  • 1
  • 18
  • 25