Skip Navigation

About This Website

CodeStore is all about web development. Mostly with the Lotus Domino server.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

About This Article

Author: Jake Howlett
Date: 2 November 2000
Article: DOMM-4QS3RL
Category: JavaScript
Keywords: Form; submit; onsubmit; validate;

Options

Feedback
Print Friendly

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.

Feedback

    • avatar
    • emo
    • Posted on Wed 1 Sep 2004

    no onSubmit

    and if I don't always have the onSubmit() ??

    • avatar
    • Juan
    • Posted on Wed 23 Feb 2005

    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 !

    • avatar
    • crab
    • Posted on Thu 24 Feb 2005

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

  1. 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.

    • avatar
    • Kip
    • Posted on Tue 16 Sep 2008

    Thanks!

    This was really confusing me and I finally found your post via Google and finally the problem is solved... thank you.

Add your own response here:

Although your email address isn't required it is protected from spambots if you choose to provide it and not to hide it. My right to remove commercial, irrelevant or posts I just don't like is reserved.

Name *:
E-mail:
Website:
 

Comment *:

HTML is not allowed!
 

Navigate other articles in the category "JavaScript"

« Previous Article Next Article »
Creating dynamic references to objects   Using the if statement effectively