Javascript Tutorials

Resetting forms using an Image Button

Resetting forms using an Image Button (Image Field)

  Home Services Portfolio Pricing Templates Web Apps Tutorials Downloads About Contact
  • Resetting forms Tutorials
    • 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)

        Name
        Address
        Email Address
       
      Get free stuff and free info. Just check the boxes that interests you.
        Free Stuff Fitness
        Clothing     Magazines
        Cosmetics   Movies 
        Entertainment   Music 

      Cut and Paste Code

      Cut 'n' paste code for the above Example

      JavaScript Code
      HTML Code
      Cut and paste the below code and then create your other fields inside the already created form. For the reset button in the given code just change the path to your image button.

      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.
Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks for your encouragement!
Help us improve! We appreciate your feedback.
Sponsored Link(s):