Know the form object, which contains an entire form with its elements. See the different ways to access the form object using Javascript. Study the properties and methods of form objects.
The first step of the manual will consist of knowing the form object, its elements and properties. Later we will see some of the different ways of processing data from forms using Javascript, as well as sending and receiving them, etc.
The object forms
Within the DOM () of Javascript we find different objects of the browser. Each of the elements that are part of a page have a representation in the DOM, among others the form object.
The form object directly depends on the document object and this in turn depends on the window object, which is the main DOM object.
Just as the AND tags are used to create an HTML page, the same thing happens with a form: the form must be contained between the
tags.
In order to distinguish the forms we have to identify them in some way. There are two basic alternatives to achieve this and depending on which one we use, we will have to access the form object in a different way. We will study each of these forms separately.
Access to the form from Javascript using its identifier
As a simpler alternative we can use an id attribute to assign an identifier to the form. This identifier has to be unique for every other element on the page. That is, there cannot be two labels with the same identifier, whether they are forms or any other label.
Given this possibility, we can access the form like this:
let FormObject = document.getElementById(‘myform’);
Access to the form by name
With this second possibility we get the same result as the previous one, accessing the form object. Only that we will do it through the name that we have indicated with the “name” attribute.
From the point of view of HTML there is a difference between putting the id and the name. While the id must be unique across all tags on the page, the name simply must be unique across all
methods of the form object
Now we are going to see the methods of the form object, through which we can access various functionality on the forms, to control them through Javascript. The form object has two methods:
submit: Submit the form.
reset: resets the form to the default values.
These methods produce the same actions as those that would be obtained by pressing the typical form buttons to submit and to reset it.
Application example with forms and Javascript
With what we have learned so far, we are going to carry out a couple of examples below, where we will see the use of the method property and the submit and reset methods.
Method POST
The code….
If we wanted to access this form to submit it, without a person having to press the submit button, we could launch this Javascript statement:
document.data.submit();
This other statement would be equivalent:
document.forms.data.submit();
Method GET
The code….
If we wanted to access this form to reset it, without a person having to press the reset button, we could launch this Javascript statement:
document.data1.reset();
This other statement would be equivalent:
document.forms.reset();
Conclusion on accessing forms using Javascript
In this article we have seen various ways to access forms with Javascript and be able to perform simple operations on them such as sending forms and resetting them.
In the next article we’ll learn more about the , which contains a list of all the forms on the page.