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.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
How To Rename A File In Python

How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…
View Post
How To Copy A File In Python

How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess…
View Post