Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:36:05+00:00 2026-06-14T04:36:05+00:00

This is probably not your usual How do I capture form submit events? question.

  • 0

This is probably not your usual “How do I capture form submit events?” question.

I’m trying to understand precisely how form submit events are handled by jQuery, vanilla Javascript, and the browser (IE/FF/Chrome/Safari/Opera) — and the relationships between them. (See my other question.) After hours of Googling and experimenting, I still cannot come to a conclusion, either because of discord or vagueness.

I’m finishing up a script which integrates with website forms so that the forms can’t be submitted until an AJAX request comes back.

Ideally:

  1. User fills out form
  2. Form is submitted — but no previously-bound event handlers are called, except mine
  3. My handler makes an API request (asynchronously, of course)
  4. User confirms validation results from API response
  5. Form submit continues normally, automatically, invoking the other handlers which before were suppressed

My current understanding is that: (these may be wrong, please correct me if so)

  • jQuery binds form submit event handlers to the submit button click events
  • Event handlers directly on the submit element click events (whether in the markup like onclick="" or bound using jQuery) are executed first
  • Event handlers on the form submit events (whether in the markup like onsubmit="" or bound using jQuery) are executed next
  • Calling $('[type=submit]').click() doesn’t invoke the form’s submit event, so its handlers don’t get called
  • Calling $('form').submit() doesn’t invoke the submit button’s click event, so its handlers don’t get called
  • Yet somehow, a user that clicks the submit button ends up invoking handlers bound to the form’s submit event… (but as mentioned above, invoking click on the submit button doesn’t do the same)
  • In fact, any way the user submits the form (via submit button or hitting Enter), handlers bound with jQuery to the form submit event are called…

Right now, I am:

  1. Unbinding handlers bound with jQuery to the submit button’s click event while preserving a reference to them
  2. Binding my own handler to the submit button’s click event, so it executes first
  3. Taking any handlers bound in the markup using onclick="" and onsubmit="" (on their respective elements) and re-binding them using jQuery (so they execute after mine), then setting the attributes to null
  4. Re-binding their handlers (from step 1) so they execute last

Actually, this has been remarkably effective in my own testing, so that my event handler fires first (essential).

The problem, and my questions:

My handler fires first, just as expected (so far). The problem is that my handler is asynchronous, so I have to suppress (preventDefault/stopPropagation/etc) the form submit or submit button click event which invoked it… until the API request is done. Then, when the API request comes back, and everything is A-OK, I need to re-invoke the form submit automatically. But because of my observations above, how do I make sure all the event handlers are fired as if it were a natural form submit?

What is the cleanest way to grab all their event handlers, put mine first, then re-invoke the form submit so that everything is called in its proper order?

And what’s the difference, if any, between $('form').submit() and $('form')[0].submit()? (And the same for $('[type=submit]').click() and $('[type=submit]')[0].click())

tl;dr, What is the canonical, clear, one-size-fits-all documentation about Javascript/jQuery/browser form-submit-event-handling? (I’m not looking for book recommendations.)


Some explanation: I’m trying to compensate for a lot of the Javascript in shopping cart checkout pages, where sometimes the form is submitted only when the user CLICKS the BUTTON (not a submit button) at the bottom of the page, or there are other tricky scenarios. So far, it’s been fairly successful, it’s just re-invoking the submit that’s really the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:36:06+00:00Added an answer on June 14, 2026 at 4:36 am

    Here’s how I ended up doing this, and it has been very successful so far in numerous test cases. I learned an awful lot about events, particularly form submit events, in relation to jQuery. I don’t have time to post a comprehensive encyclopedia of all the information I collected, but this will suffice for now:

    This was for the SmartyStreets LiveAddress API jQuery Plugin which validates addresses before the user leaves the page.

    The most successful method was by grabbing the submit button’s click event. The snippet below is found in the jquery.liveaddress.js file. It gets references to as many event handlers as possible (jQuery, onclick — the onclick ones fire first), uproots them, plops down its own (submitHandler), and layers the others on top of it. It’s worked successfully on sites like TortugaRumCakes.com (checkout) and MedicalCareAlert.com (homepage and checkout) as well as many others.

    The full code is on GitHub. This particular segment goes for the "click" on the submit button, but similar code is used to handle form submit also. jQuery’s submit() function seems to be rather proprietary… but this handling both ensures it gets called even when .submit() is called programmatically on a jQuery object.

    var oldHandlers, eventsRef = $._data(this, 'events');
    
    // If there are previously-bound-event-handlers (from jQuery), get those.
    if (eventsRef && eventsRef.click && eventsRef.click.length > 0)
    {
        // Get a reference to the old handlers previously bound by jQuery
        oldHandlers = $.extend(true, [], eventsRef.click);
    }
    
    // Unbind them...
    $(this).unbind('click');
    
    // ... then bind ours first ...
    $(this).click({ form: f, invoke: this }, submitHandler);
    
    // ... then bind theirs last:
    // First bind their onclick="..." handles...
    if (typeof this.onclick === 'function')
    {
        var temp = this.onclick;
        this.onclick = null;
        $(this).click(temp);
    }
    
    // ... then finish up with their old jQuery handles.
    if (oldHandlers)
        for (var j = 0; j < oldHandlers.length; j++)
            $(this).click(oldHandlers[j].data, oldHandlers[j].handler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably not a rocket science question, so forgive me for being a
I'm used to MSSQL, not Mysql, so sorry for this probably stupid question. I'm
i'm very aware that this is probably not a stackoverflow question, but i figure
This is probably not a dup; I have read through many similar problems on
Okay, I know that 1) this is probably not possible with CSS alone and
This is weird and probably not possible but I'll ask anyway. I'm making this
I know this probably really simple but Im not sure what im doing wrong...
I'm doing this as part of a Java tutorial, so it is probably not
I'm sure there's a clean way to do this, but I'm probably not using
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.