Question

I'm having a little problem with jQuery: I'm having .submit() defined on a form, when I submit the form by clicking the submit button, the even triggers and things works smoothly, however, when I use document.forms[0].submit(), the event doesn't get triggered, but the form submits!! Is there any reason why? How can I overcome that?

Was it helpful?

Solution

That's just the way it works - calling the native .submit() doesn't trigger the javascript event handler.

Use the jQuery .submit() instead:

$('form:first').submit();

OTHER TIPS

If the submit form is named like this

<form method="POST" id="the_waffle_form">

Then you can submit it by doing this.

$("#the_waffle_form").submit();

Check out the documentation for more great examples.

There are some of us out here that are forced to use code from legacy systems. These systems are non-compliant, and in my case awaiting to be upgraded. But when several thousands of people are using the system, it's just not possible to go in and change code to baseline packages. So, we are forced to find workarounds. In my case, the forms do not have IDs or names, so using document.forms[0].submit() is one of the only options.

This may sound strange but why do you even use document.forms[0].submit() when you use jQuery already.

Simple (maybe stupid?) suggestion: Don't use document.forms[0].submit() use this instead

$("#hereidofyourform").submit()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top