By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

onSubmit not working

P: n/a
Hi,

I have a form with onSubmit embedded in the <form>
tag. The form is submitted programatically through
javascript [ie... form.submit()]. While the form submits fine, nothing
I'm doing seems to get it to also register the code in the onSubmit event.
Any suggestions as to why this is happening or how to resolve it?

-Stu
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Stuart Wexler wrote:
Hi,

I have a form with onSubmit embedded in the <form>
tag. The form is submitted programatically through
javascript [ie... form.submit()]. While the form submits fine, nothing
I'm doing seems to get it to also register the code in the onSubmit event.
Any suggestions as to why this is happening or how to resolve it?

-Stu


Yes, this is correct behavior. the onsubmit event does not fire if you call
document.forms['yourForm'].submit();

The way to get around this is to add the onsubmit validation test as a
condition before calling submit(). So instead of:

<a href="#"
onclick="document.forms['yourForm'].submit();return false;">Submit</a>

you would use:

<a href="#"
onclick="
var f = document.forms['yourForm'];
if (validate(f)) {
f.submit();
}
return false;
">Submit</a>

Indentation and newlines for readability only, it can all go on a single line,
or be put in a function and called from the onclick event.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2

P: n/a
In article <C_********************@comcast.com>, St*******@comcast.net
enlightened us with...
Hi,

I have a form with onSubmit embedded in the <form>
tag. The form is submitted programatically through
javascript [ie... form.submit()]. While the form submits fine, nothing
I'm doing seems to get it to also register the code in the onSubmit event.
Any suggestions as to why this is happening or how to resolve it?


For whatever reason, onSubmit won't execute from form.submit(). I had
that problem, too. I never found out if it was a bug or just one of
those things that doesn't make any sense.
You have to call the function that onSubmit calls from the same function
that calls form.submit().

-------------------------------------------------
~kaeli~
Black holes were created when God divided by 0.
Not one shred of evidence supports the notion
that life is serious.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
Jul 20 '05 #3

P: n/a
On Mon, 21 Jul 2003 17:36:06 GMT, Grant Wagner
<gw*****@agricoreunited.com> wrote:
Stuart Wexler wrote:
Hi,

I have a form with onSubmit embedded in the <form>
tag. The form is submitted programatically through
javascript [ie... form.submit()]. While the form submits fine, nothing
I'm doing seems to get it to also register the code in the onSubmit event.
Any suggestions as to why this is happening or how to resolve it?

-Stu


Yes, this is correct behavior. the onsubmit event does not fire if you call
document.forms['yourForm'].submit();


It's documented correct in IE, but to standards it's wrong, a lot of
pages would break right now if the correct spec following was done...

I'd play safe and always call onsubmit manually, then remove it, then
submit.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #4

P: n/a
Jim Ley wrote:
On Mon, 21 Jul 2003 17:36:06 GMT, Grant Wagner
<gw*****@agricoreunited.com> wrote:
Stuart Wexler wrote:
Hi,

I have a form with onSubmit embedded in the <form>
tag. The form is submitted programatically through
javascript [ie... form.submit()]. While the form submits fine, nothing
I'm doing seems to get it to also register the code in the onSubmit event.
Any suggestions as to why this is happening or how to resolve it?

-Stu


Yes, this is correct behavior. the onsubmit event does not fire if you call
document.forms['yourForm'].submit();


It's documented correct in IE, but to standards it's wrong, a lot of
pages would break right now if the correct spec following was done...

I'd play safe and always call onsubmit manually, then remove it, then
submit.

Jim.


I asked a while back about this and received a reply at the time that seemed to
indicate onsubmit should not fire when submit() is called, but it's certainly
possible the person responding was incorrect.

As for browser support, IE 4+, Netscape 3+ and Opera up to 6 did not call onsubmit
when submit() was invoked. It seems that Opera 7.x does, which can lead to all
sorts of nasty surprises if you both manually call the validation routine and have
it called as part of an onsubmit event.

So you're right, remove the onsubmit event from the form and always call it
manually if you are invoking submit() anywhere. Better yet, avoid calling submit()
directly wherever possible.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.