Languages
[Edit]
EN

JavaScript - submit form example

0 points
Created by:
Maison-Humphries
791

In this article, we would like to show you how to submit a form in JavaScript.

Quick solution:

// add onsubmit event to the form, specify submit handling function and form fields
<form id="my-form" onsubmit="submitHandlingFunction(event)">
    Form field: <input type="text">
    <input type="submit" />
</form>

 

Practical example

In this example, we will submit my-form. To do so, we use onsubmit event and submitForm() event handling function.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
  <body>
    <form id="my-form" onsubmit="submitForm(event)">
      Username: <input type="text" /><br />
      Password: <input type="password" /><br />
      <input type="submit" value="Submit" />
    </form>
    <script>

      var myForm = document.querySelector('#my-form');

      function submitForm(event) {
        event.preventDefault();
        console.log('onsubmit event occurred...');
      }

    </script>
  </body>
</html>

Related posts

Alternative titles

  1. JavaScript - onsubmit event example
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join