Results 1 to 5 of 5

Thread: How to Reset or Clear a form using JavaScript?

  1. #1
    Join Date
    Jul 2006
    Posts
    182

    How to Reset or Clear a form using JavaScript?

    Hi friends,
    I am new to the scripting. Recently I have started doing the JavaScript. I have designed the form in HTML. But for making the form more advanced I will have to use the JavaScript. Now I have stuck while writing the script for the reset button. I want the reset button on my form so that the details filled into it should be cleared which are seen in almost websites. So anyone hanging out there can explain me how to reset or clear a form using JavaScript?. Please help me soon.!!
    "Yea though I walk through the valley of the shadow of death... I will fear no evil." -Psalms 23

    K8N Diamond Plus (BIOS v1.2)
    AMD Athlon 64 X2 4400+
    Antec TruControl 550W
    NVidia GeForce 7900GT (NGO v1.8466 BETA)
    OCZ Platinum 2x1GB (2-3-2-5)
    SATA: WD740
    PATA: 2xWD2500, WD1200, NEC DVD/RW

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: How to Reset or Clear a form using JavaScript?

    You use the reset button to erase the details filled by the user and sometimes it keeps the default settings. Using an HTML 'Reset' button is an easy way to reset all form fields to their default values. So I thought that providing the sytnax type code would be more helpful to understand. The code that I have mentioned below shows an <input> field of type "reset", which on being clicked resets all the form fields :
    HTML Code:
    <input type="reset" value="Reset Form">
    The other method for doing the same thing is by using the form's reset() method along with the simple button element. Here is the code for that :
    HTML Code:
     <input type="button" value="Reset Form" onClick="howto.reset()" />

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to Reset or Clear a form using JavaScript?

    You can look at an example of a simple form below :
    HTML Code:
    <form name="salary_entry" action="#">
    
    Company Name: <input type="text" size="35" name="company_name">
    
    Select Business Type: <input type="radio" name="class_category" value="1"> Typer
    <input type="radio" name="class_category" value="2"> Clerk
    <input type="radio" name="class_category" value="3"> Assistant Manager 
    <input type="radio" name="class_category" value="4"> Manager
    
    Email Address: <input type="text" size="30" name="email">
    
    Keep Information Private: <input type="checkbox" name="privacy">
    
    <input type="button" name="reset_form" value="Reset Form" onclick="this.form.reset();">
    
    <input type="button" name="clear" value="Clear Form" onclick="clearForm(this.form);">
    </form>

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to Reset or Clear a form using JavaScript?

    You should keep in mind that to acquire a reference to all the elements in the form object, you would have to use the following :
    Code:
    var frm_elements = oForm.elements;
    where, oForm is a reference to the form object passed to the function using this.form. oForm.elements can also be Javascript array that holds a reference to each of the form elements.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to Reset or Clear a form using JavaScript?

    At times the new developers get confused between the reset and clear options. So I have provided you with the following snippet of code which sets the default for the radio element named "business_category" to the value 3. Check the coding :
    HTML Code:
    <input type="radio" name="business_category" value="1">Manufacturer
    <input type="radio" name="business_category" value="2">Whole Sale Supplier
    <input type="radio" name="business_category" value="3" checked>Retailer
    <input type="radio" name="business_category" value="4">Service Provider

Similar Threads

  1. using javascript how to clear text area?
    By talia in forum Software Development
    Replies: 4
    Last Post: 03-02-2014, 04:26 PM
  2. Calendar javascript in a JSP form
    By technika in forum Software Development
    Replies: 4
    Last Post: 22-02-2010, 07:10 PM
  3. How to set the value of a form element using Javascript?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 07-02-2010, 05:04 AM
  4. Php form with javascript
    By Solaris in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 02:22 PM
  5. How to clear browser form fields
    By Rover in forum Software Development
    Replies: 3
    Last Post: 29-08-2009, 09:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,559,543.04866 seconds with 16 queries