Code Snippet: How to submit and reset a Form using JavaScript

The standard approach to submit a form is through the use of the submit button. However we may find that we may want to utilize an image, text link or other elements to submit a form. In these cases we can utilize JavaScript to submit the form for us through the use of the JavaScript form object.

When your html page contains a form then a form object is created in your document that can be accessed by its name.  The form object contains both the submit and reset functions. To access the form object we use the notation document.FORM_NAME.

Examples

HTML Form

<form name='mailForm' method="POST">
   // form contents
</form>

JavaScript Form Submit

<a href="javascript: document.mailForm.submit()">Send mail</a>

JavaScript Form Reset

<a href="javascript:  document.mailForm.reset()">Clear</a>

Javascript Method

<SCRIPT language="JavaScript">
  function sendEmail ()
  {
     document.mailForm.submit();
  }
</SCRIPT>

<a href="javascript: sendEmail()">Send mail</a>

Comments & Questions

Add Your Comment