Languages
[Edit]
EN

JavaScript - submit form example

0 points
Created by:
Dirask Community
6240

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

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