Personal tools
Home Webmaster Resources JavaScript Submit a Form with JavaScript

Submit a Form with JavaScript

If you want to spare your users the extra step of clicking a submit button, you can use JavaScript to submit a form when a certain user action takes place.

You can use JavaScript to submit a form by calling the form's submit method.  Shown below are a few different ways of handling it:

Fewest Lines of Code
<form name="agencyselect">
    <select name="agency" onChange="javascript: document.agencyselect.submit()">
          <option>Agency #1</option>
          <option>Agency #2</option>
    </select>
</form>



Better Style
<form name="agencyselect">
    <select name="agency" onChange="submitForm()">
          <option>Agency #1</option>
          <option>Agency #2</option>
    </select>
</form>

<script>
function submitForm() {
    document.agencyselect.submit();
}
</script>



As seen in both examples above, the first step is to give your form a name so it can be accessed through JavaScript.  Once you name the form, it becomes accessible as a child of the document object.

Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.