Skip to content

JavaScript submit form | Example code

  • by

To create a form in HTML, you use the <form> element and to use submit() function to submit the form using JavaScript.

<form action="/signup" method="post" id="signup"> 
</form>

Submit the form without using the name the tag inside the form:

HTML

<button type="submit" onClick="placeOrder(this.form)">Place Order</button>

JavaScript

function placeOrder(form){
    form.submit();
}

Complete code

<!DOCTYPE html>
<html>
<body>
  <form method="post" action="yourUrl" name="theForm">
    <button onclick="placeOrder()">Place Order</button>
  </form>


  <script>
   function placeOrder () {
    document.theForm.submit() 
  }

  </script>

</body>
</html>

Output:

JavaScript submit form

Another Example



<html>

<body>

  <p>Enter some text in the fields below, and then press the "Submit form" button to submit the form.</p>

  <form id="myForm" action="/action_page.php">
    First name: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br><br>
    <input type="button" onclick="myFunction()" value="Submit form">
  </form>

  <script>
    function myFunction() {
      document.getElementById("myForm").submit();
      alert("submit")
    }
  </script>

</body>
</html>

Do comment if you have any doubts or suggestions on this JS form topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading