Form Handling in JavaScript
Form Recap
-
Forms allow users to interact with a Web page by being able to provide information to the system in a way that goes beyond simply pointing and clicking things with the mouse.
-
They allow the user to provide feedback to the Web page.
-
JavaScript is one of many ways to process and respond to user feedback by evaulating the information provided in the form by the user.
Accessing Form Object
To work with Form we need to access it in our JavaScript code.
This can be done in following ways:
-
Using forms[ ] array.
-
Using name of the form
-
Using getElementById( ) method
The forms[ ] Array
-
When the browser builds our web page then it stores all the form objects in an array.
-
To a programmer this array is made available as a property of document object called forms.
-
Hence by writing document.forms we get an array of all form objects on our web page
For Example
var myforms=document.forms;
Now in the above code saying:
myforms[0] will give us access to first form
myforms[1] will give us access to second form
and so on.
Using the “name” property
We can also access a form by using it’s name property value with document object.
For Example
<form name=“frmlogin” . . . . .>
. . . . .
</form>
Now in JavaScript we can access this form as:
var myform=document.frmlogin;