469,609 Members | 1,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Form onsubmit

If you have an onsubmit event handler defined for a form, it doesn't seem to
trigger if you manually call the .submit() method of the form object. Is
there any way you can make sure that code is executed?

thnx,
Christoph
Mar 14 '08 #1
2 6423
On Mar 14, 10:15 am, "Sorrow" <jcbo...@yahoo.comwrote:
If you have an onsubmit event handler defined for a form, it doesn't seem to
trigger if you manually call the .submit() method of the form object.
Most browsers work that way, but some will fire the submit event when
the form's submit method is called programatically (e.g. Opera 7,
perhaps later versions too, I can't test it right now).

Is
there any way you can make sure that code is executed?
Call it with the function that calls submit. You will need to deal
with stopping the onsubmit handler from firing twice in some browsers
- perhaps if it runs successfully, you can remove it before calling
submit().
<script type="text/javascript">

function doSubmit(el) {
var form = el.form;
if (typeof form.onsubmit == 'function') {
if (form.onsubmit() !== false) {
form.submit();
}
}
}

</script>

<form onsubmit="alert('hey'); return false;">
<div><input type="button" value="submit onclick"
onclick="doSubmit(this);"></div>
</form>

--
Rob
Mar 14 '08 #2
SAM
RobG a écrit :
>
Call it with the function that calls submit. You will need to deal
with stopping the onsubmit handler from firing twice in some browsers
- perhaps if it runs successfully, you can remove it before calling
submit().
<script type="text/javascript">

function doSubmit(el) {
var form = el.form;
if (typeof form.onsubmit == 'function') {
if (form.onsubmit() !== false) {
I do not understand at all why :
form.submit();
now runs as waited ...

or is it the form.onsubmit() that fires ?
}
}
}
Ha ! it is :

function doSubmit(el) {
var form = el.form;
form.onsubmit();
}

that does the job (in my Fx)
>
</script>

<form onsubmit="alert('hey'); return false;">
<div><input type="button" value="submit onclick"
onclick="doSubmit(this);"></div>
</form>
--
sm
Mar 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by greg brant | last post: by
12 posts views Thread by CJ | last post: by
2 posts views Thread by JHB | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.