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.submit()

//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>
</form>

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

 

0 Shares
Leave a Reply

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

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…