How to Submit a Form using JavaScript

The easiest way to submit a form using JavaScript is by using HTMLFormElement.submit() method. This method does something similar to activating a submit button of the form.

If a form control (such as a submit button) has a name or id of submit it will mask the form’s submit method.

Syntax to Submit a Form using JavaScript


//HTMLFormElement is nothing but your formObject

Example to Submit a form using JavScript

<form name="myform" class="myform" action="mail.php">
Your name:<br>
<input type="text" name="myform-name"><br>
<button type="submit">Submit</button>

<script type="text/javascript">document.myform.submit();</script>

Alternatively, you could also use document.getElementById("myForm").submit();

Similarly, you could use document.getElementsByClassName("myForm_classname").submit();  to submit form using class name and document.getElementsByTagName("form").submit(); incase if you would like to submit form using tag name.

The submit() method submits the form (same as clicking the Submit button).


Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like