Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
26 Feb 2010 1  
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

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here