Continue a jquery interrupted form submit

Published: 16-May-2017
Author: Steven Neiland
Site Url: http://www.neiland.net/article/continue-a-jquery-interrupted-form-submit/

Normally when using JQuery to block a form submit event you would do so because the user has failed to fill out something correctly. In this case you just block the submit event, display your errors and call it done. Something like this...

$(document).ready(function(){
      $('#myform').on('submit',function(event){

            // Validate the form
            ...

            if( validationFailed ){
                  // show your error messages
                  ...

                  // prevent form submit completing
                  event.preventDefault();
            }
      });
});

Continue the interrupted form submit

However there will be occasions where you may need to continue the form submission process after something has completed. While not well documented, this is actually very easy to accomplish. All you need to do is call the currenttarget submit like this:

$(document).ready(function(){
      $('#myform').on('submit',function(event){
            // block form submit event
            event.preventDefault();

            // Do some stuff here
            ...

            // Continue the form submit
            event.currentTarget.submit();
      });
});

This is most often useful when dealing with async workflows i.e. ajax

$(document).ready(function(){
      $('#myform').on('submit',function(event){
            // block form submit event
            event.preventDefault();

            // Do something ajaxy
            $.ajax({
                  success: function( data ){
                        // Continue the form submit
                        event.currentTarget.submit();
                  }
            });
      });
});