faqts : Computers : Programming : Languages : JavaScript : Forms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

29 of 33 people (88%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do I reset all forms in a document including those in layers?

Sep 14th, 2000 08:04
David Gurney, Martin Honnen, Flemming V. Larsen,


You need to loop through 
  document.forms
and for NN4 additionally recursively through
  document.layers
and contained forms. The following page contains a function
  resetAllForms
that does that. Example call and exampe forms are included
<HTML>
<HEAD>
<STYLE>
.layer { position: relative; }
</STYLE>
<SCRIPT>
function resetAllForms (windowOrLayer) {
  if (!windowOrLayer)
    windowOrLayer = window;
  for (var f = 0; f < document.forms.length; f++)
    windowOrLayer.document.forms[f].reset();
  if (document.layers)
    for (var l = 0; l < windowOrLayer.document.layers.length; l++)
      resetAllForms(windowOrLayer.document.layers[l]);
}
</SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript: resetAllForms(); void 0">
reset all forms
</A>
<FORM>
<INPUT TYPE="text">
</FORM>
<DIV CLASS="layer">
<FORM>
<INPUT TYPE="text">
</FORM>
<DIV CLASS="layer">
<FORM>
<INPUT TYPE="text">
</FORM>
</DIV>
</DIV>
</BODY>
</HTML>
this doesn't work in NN4 if ALL forms are in layers
a slightly more flexible function to cope with NN4-6 and IE4-5.5 would 
be
function resetAllForms (windowOrLayer) {
  if (!windowOrLayer)
    windowOrLayer = window;
  if (document.forms.length>0){
  for (var f = 0; f < document.forms.length; f++)
        windowOrLayer.document.forms[f].reset();
  }
  if (document.layers){
    for (var l = 0; l < windowOrLayer.document.layers.length;l++ )
       resetAllForms(windowOrLayer.document.layers[l]);
  }
}