The form Submit method and event
Anybody with any programming experience starting to use JavaScript would expect that when they call a form's submit() method the corresponding onSubmit() event would be triggered. This is not the case.
For example, say that a document has the following function in its JS Header:
function FormSubmit( formObjecct ){
if ( formObject.Country.value=="") {
alert('You need to the Country field in');
formObject.Country.focus();
return false
} else {
return true
}
}
and the document has a form whose tag is:
<form name="_prj" onSubmit="FormSubmit( this )" method="post" action="....
This is all well and good as long as you use a "standard" submit button on the form:
<input type="submit" name="Submit" value="Submit This Form">
But if you needed to call this Submit method explicitly, from a function, button, anchor link etc then calling this method does not call the onSubmit() event:
<a href="JavaScript: document._prj.submit();">Submit Form</a>
In order to validate the form first the code would need to be:
if ( document._prj.onsubmit() ) document._prj.submit();
Hence:
<a href="JavaScript: if (document._prj.onsubmit()) document._prj.submit();">Submit Form</a>
Note: In R4.6 if you have a button that uses the following formula:
@Command([FileSave]); @Command([FileCloseWindow])
The resulting JavaScript calls the Submit method whilst ignoring the onSubmit event.
In R5 this has been rectified and the same button produces script that accounts for the onSubmit.
no onSubmit
and if I don't always have the onSubmit() ??
Reply
This is unlogical !!
I had the problem with the submit method, and i find this so unlogical that it takes me time to think it could be that ! But all you write is totaly write, thanks !
Reply
i simply do this
I simply do this to submit the form and invoke the onSubmit event by Javascript:
function submitFormWithOnSubmitEventFired(f) { if (f.onsubmit()) { f.submit(); } }
Reply
Doesn't work with dynamically added events
Correct me if I'm wrong, but calling form.onsubmit() works great only IF you've actually got an onsubmit event specified in the form tag as <form onsubmit="somefunction()">. But if you add events programmatically, e.g. addEventListener, this seems to fail.
Reply
Thanks!
This was really confusing me and I finally found your post via Google and finally the problem is solved... thank you.
Reply