Search code examples
jquery-pluginsjqueryjquery-forms-plugin

jQuery Form plugin ajaxSubmit callback timing


I have the following code bound to the click event of a button:

function submitForm() {

    var $submitOK = false;
    $('#form1').ajaxSubmit({
        beforeSubmit: function() {
            $submitOK = $('#form1').valid();
            return $submitOK;
        },
        success: function(html, status) {
            $("#response").text(html.substring(1, html.length - 1));            
        }
    });

    if ($submitOK) {
        processForm1();
    }

    return $submitOK;
}

How does the timing of the ajaxSubmit and the beforeSubmit callback play out - will the beforeSubmit callback always return before execution returns from the ajaxSubmit()?

The code behaves as desired - returning $submitOK equal to the result of $('#form1').valid(), but I wanted to make sure this is deterministic behaviour.

Thanks.


Solution

  • Yes, beforeSubmit will always return first, that's it's purpose - it is a pre-submission hook.