How to Execute an HTML Form Using JavaScript

Most of websites nowadays have at least one form. In general, the form is executed when a user presses a submit/send button. However, sometimes, you may need to submit the form programmatically using JavaScript without the interaction of a user. User wouldn’t even know because the JavaScript executes in the back-end.

JavaScript provides the form object that contains the submit() method. You will have to use the id or name of the form to get the form object.

For example, if the name of your form is ‘thisform’, the JavaScript code for the submit call is:
document.forms["thisform"].submit();

Here is the code to submit a form when a hyperlink is clicked:

<form name=”thisform” action=”process_form.php” method=”post”>
Search: <input type=’text’ name=’query’ />
<a href=”javascript: submitthisform()”>Search</a>
</form>
<script type=”text/javascript”>
function submitthisform()
{
document.thisform.submit();
}
</script>