Click here to Skip to main content
12,394,877 members (69,381 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.9K views
2 bookmarked
Posted

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

, 26 Feb 2010 CPOL
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...

Comments and Discussions

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