Click here to Skip to main content
13,193,770 members (63,679 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.1K views
2 bookmarked
Posted 26 Feb 2010

Simple and easy way to clear all the input fields in the form.

, 26 Feb 2010
Rate this:
Please Sign up or sign in to vote.
ALTERNATIVE #2: via JavaScript function ClearAllControls() { for (i=0; i<document.forms[0].length; i++) { doc = document.forms[0].elements[i]; ...
ALTERNATIVE #2: via JavaScript

<script language="javascript" type='text/javascript'>
 
        function ClearAllControls() 
        {
              for (i=0; i<document.forms[0].length; i++)
              {
                    doc = document.forms[0].elements[i];
                    switch (doc.type) 
                    {
                        case "text" :
                                doc.value = "";
                                break;
                          case "checkbox" :
                                doc.checked = false;
                                break;    
                          case "radio" :
                                doc.checked = false;
                                break;                
                          case "select-one" :
                                doc.options[doc.selectedIndex].selected = false;
                                break;                      
                          case "select-multiple" :
                                while (doc.selectedIndex != -1) 
                                {
                                      indx = doc.selectedIndex;
                                      doc.options[indx].selected = false;
                                }
                                doc.selected = false;
                                break;
                                    
                          default :
                                break;
                    }
              }
        }
 
</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Current location: Bangalore, India.

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171018.2 | Last Updated 27 Feb 2010
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid