/  Technology   /  Can you submit a form using JavaScript?
submit

Can you submit a form using JavaScript?

 

Declarative HTTP requests can be sent using HTML forms. And forms may also construct an HTTP request for sending through JavaScript, such as XMLHttpRequest.

To submit a form, use the form.submit() method in the JavaScript onclick event. You can perform a submit action by clicking on a submit button, a hyperlink, a button, or an image tag, for example. You can also perform JavaScript form submission using form attributes such as id, name, class, and tag name.

Gathering all of the information that was submitted into the form and sending it to another application for processing. The form was submitted by clicking the button. The bottom of most HTML forms has a button. The name of the button can be altered. The button type, however, is always submit.

  1. Create an HTML form with the <form> element.
  2.  To create form elements you have DOM techniques such as getElementById() and querySelector().
  3. To access form elements, use form.elements.
  4. Theform. submit () JavaScript method is triggered when users click the form’s submit button.

JavaScript form code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Form</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <form action="signup.html" method="post" id="signup">
        <h1>Sign Up</h1>
        <div class="field">
          <label for="name">Name:</label>
          <input type="text" id="name" name="name" placeholder="Enter your fullname" />
          <small></small>
        </div>
        <div class="field">
           <label for="email">Email:</label>
           <input type="text" id="email" name="email" 
                      placeholder="Enter your email address" />
           <small></small>
         </div>
         <div class="field">
           <button type="submit" class="full">Login</button> 
         </div>
        </form>
      </div>
    <script src="app.js"></script>
  </body>
</html>

 

Leave a comment