Click here to Skip to main content
11,709,537 members (50,891 online)
Click here to Skip to main content

Tagged as

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

, 26 Feb 2010 CPOL 3.9K 2
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
Web03 | 2.8.150819.1 | Last Updated 27 Feb 2010
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid