Resetting forms using an Image Button
In forms when using text boxes or text areas and check boxes you might need a reset button that is not the usual gray HTML button. For this you can use an image field and a small JavaScript function as given below.
Note: This button will reset only text boxes,text areas and check boxes as list boxes and option buttons don't usually require resetting. After resetting the form the cursor will set the focus to the first field in the form.
Example
Example of resetting a form using an image button (image field)
Cut and Paste Code
Cut 'n' paste code for the above Example
Explanation of the Code
There is just one JavaScript function ResetForm. The logic behind the functioning of this code is very simple.
- The JavaScript first checks each element in the form submitted
to see if it is a text field, text area or check box
by checking the type property for a 'text field or checkbox'
and checking the col property (no. of columns) for a
'text area' field. If found we set the field's value to null
(for text boxes or text areas) or unchecked (for check boxes).
- After resetting all values found it sets the focus to the first text, text area or checkbox field in the form. This is done by getting the index of the first form element encountered which was a text field, text area or check box and storing it in a variable called first. This variable is then used to set the focus.