onSubmit not working

S

Stuart Wexler

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
 
K

kaeli

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
-------------------------------------------------
 
G

Grant Wagner

Stuart said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
J

Jim Ley

Stuart said:
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.
 
G

Grant Wagner

Jim said:
Stuart said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top