JavaScript Form

In this tutorial, we will learn, discuss, and understand the JavaScript form. We will also see the implementation of the JavaScript form for different purposes.

Here, we will learn the method to access the form, getting elements as the JavaScript form's value, and submitting the form.

The <form> element has two important attributes: action and method.

  • action is a URL that will process the form submission. In this case, it is /signup
  • method is the HTTP method to submit the form with. Typically, it is the post or get method. The post method sends data to the server as the request body while the get method appends the form data to the action URL with a ? operator.

In JavaScript, the form is represented as the HTMLFormElement object. The HTMLFormElement has the following corresponding properties:

  • action – the URL that will process the form data. It is equivalent to the HTML action attribute
  • method – can be get or post, which is equivalent to the HTML method attribute.

The HTMLFormElement element also provides the following useful methods:

  • submit() – submit the form.
  • reset() – reset the form.

Example -

Example -