khelll
khelll

Reputation: 24010

jQuery .submit problem

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?

Upvotes: 1

Views: 4117

Answers (4)

NMB
NMB

Reputation: 1

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.

Upvotes: 0

jitter
jitter

Reputation: 54605

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()

Upvotes: -1

Ólafur Waage
Ólafur Waage

Reputation: 70001

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.

Upvotes: 1

Greg
Greg

Reputation: 321766

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();

Upvotes: 5

Related Questions