37

I have a form that I would like to submit via JavaScript. Obviously this can be achieved by:

document.getElementById("myForm").submit();

The issue is that I have a listener on the submit event that looks like this:

document.getElementById("myForm").addEventListener("submit", function(event) {
    event.preventDefault();
    // Other work
});

This event handler is not triggered when I call .submit(). From MDN:

The form's onsubmit event handler (for example, onsubmit="return false;") will not be triggered when invoking this method from Gecko-based applications. In general, it is not guaranteed to be invoked by HTML user agents.

So, given this restraint, how can I submit the form in a way that ensures the event handler is invoked?

3
  • Just call dispatchEvent alongside your .submit() call Commented Feb 2, 2016 at 13:16
  • What is the proper way to to call dispatchEvent? According to MDN, dispatchEvent is supported across browsers, but calling new Event() is not. How else can an event be constructed? Commented Feb 2, 2016 at 13:38
  • probably something in line of var evt = document.createEvent("Event"); evt.initEvent("submit", true, true); form.dispatchEvent(evt); link Commented Feb 2, 2016 at 14:05

3 Answers 3

30

Update Nov 21, 2022

I would now recommend the requestSubmit() method. You can pass a submit button to it if you want. Its cleaner and can be intercepted.

document.getElementById("myForm").requestSubmit();

Original answer

A custom event works just fine.

document.getElementById("myForm").dispatchEvent(new CustomEvent('submit', {cancelable: true}));
Sign up to request clarification or add additional context in comments.

Comments

14

You need create a submit event, then dispatch it.

(function () {
              if ( typeof window.CustomEvent === "function" ) return false;

              function CustomEvent ( event, params ) {
                params = params || { bubbles: true, cancelable: true, detail: undefined };
                var evt = document.createEvent('submit');
                evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
                return evt;
               }

              CustomEvent.prototype = window.Event.prototype;

              window.CustomEvent = CustomEvent;

})();
var evt = new CustomEvent("submit", {"bubbles":true, "cancelable": true});
document.getElementById("myForm").addEventListener("submit",function(event) {
                event.preventDefault();
                alert('submit');
});

Then when you want submit this function you need to call:

!document.getElementById("myForm").dispatchEvent(evt);

For more event info see dispatchEvent.

2 Comments

According to MDN, new Event is not supported in IE: developer.mozilla.org/en-US/docs/Web/API/Event/Event
@Max, developer.mozilla.org/en-US/docs/Web/API/CustomEvent/… ,here have a common methed to deal with IE > 9
-11

Use jQuery to submit the form and add the handler.

$("#myForm").on("submit", function(e){
  e.preventDefault();
  // Handle submission here
});

// Then later
$("#myForm").submit();

5 Comments

Thanks for the answer, but I'm not using jQuery.
What exactly are you trying to do, if your trying to run some code after the form is submitted, and your submitting the form using JavaScript, then why not just run the code after you submit the code using JavaScript
I've decided to run to code after submitting the form with JavaScript. It's just somewhat annoying that you can't trigger the submit handler via .submit().
And that is exactly why people use jQuery. Because each browser has it's little quarks as to what works and what doesn't, and jQuery makes all features work on all browsers.
The .submit() works without jQuery too. For example, document.querySelector('form').submit(); works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.