Generic clearForm function when click CLEAR button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    Generic clearForm function when click CLEAR button

    I want to write a generic clearForm function to clear the form when
    the user click CLEAR button.

    Here's my attempts, because I want to take care all html controls. I
    think I need to test
    if the control is submit button, regular button. But I don't know
    what I should do on drop down box?

    function clearForm()
    { var i=0;
    for (i=0; i<InputForm.ele ments.length-1; i++)
    { var obj = InputForm.eleme nts[i];
    document.write( obj.type); //runtime error: object doesn't support
    this property or method
    if (obj.type != "submit" && obj.type != "button")
    obj.value = "";
    }
    }

    any ideas?? thanks!!
  • Mick White

    #2
    Re: Generic clearForm function when click CLEAR button

    Matt wrote:[color=blue]
    > I want to write a generic clearForm function to clear the form when
    > the user click CLEAR button.
    >
    > Here's my attempts, because I want to take care all html controls. I
    > think I need to test
    > if the control is submit button, regular button. But I don't know
    > what I should do on drop down box?
    >
    > function clearForm()
    > { var i=0;
    > for (i=0; i<InputForm.ele ments.length-1; i++)
    > { var obj = InputForm.eleme nts[i];
    > document.write( obj.type); //runtime error: object doesn't support
    > this property or method
    > if ()
    > obj.value = "";
    > }
    > }
    >
    > any ideas?? thanks!![/color]


    function clearForm(form) {
    f=form.length
    for (i=0; i<f-1; i++){
    obj=form[i];
    if(obj.value && obj.type != "submit" && obj.type != "button"){
    obj.value="";
    }
    }
    }
    You could hit "reset" button to do the same thing.
    Mick

    Comment

    • bruce

      #3
      Re: Generic clearForm function when click CLEAR button

      jrefactors@hotm ail.com (Matt) wrote in message news:<ba8a039e. 0405261948.7047 8405@posting.go ogle.com>...[color=blue]
      > I want to write a generic clearForm function to clear the form when
      > the user click CLEAR button.
      >
      > Here's my attempts, because I want to take care all html controls. I
      > think I need to test
      > if the control is submit button, regular button. But I don't know
      > what I should do on drop down box?
      >
      > function clearForm()
      > { var i=0;
      > for (i=0; i<InputForm.ele ments.length-1; i++)
      > { var obj = InputForm.eleme nts[i];
      > document.write( obj.type); //runtime error: object doesn't support
      > this property or method
      > if (obj.type != "submit" && obj.type != "button")
      > obj.value = "";
      > }
      > }
      >
      > any ideas?? thanks!![/color]



      Here's a function I wrote a zillion years ago. I offer it to you with
      no guarantees, but maybe it will help you:

      function ClearFields ()
      {
      var iIdx,iCount,oOb j,sRes,sType
      iCount=document .forms(0).lengt h
      for (iIdx=0;iIdx<iC ount;iIdx++)
      {
      oObj=document.f orms(0).item(iI dx)
      sType=oObj.type .substring(0,5)
      // alert("stype=" + oObj.type + " val=" + oObj.value)
      if (sType=="selec" )
      {
      oObj.selectedIn dex=0
      }
      else
      if (sType=="check" )
      oObj.checked=fa lse
      else
      if (sType=="text")
      oObj.value=""
      else
      if (sType=="radio" )
      oObj.checked=tr ue
      }
      }

      Comment

      • bruce

        #4
        Re: Generic clearForm function when click CLEAR button

        jrefactors@hotm ail.com (Matt) wrote in message news:<ba8a039e. 0405261948.7047 8405@posting.go ogle.com>...[color=blue]
        > I want to write a generic clearForm function to clear the form when
        > the user click CLEAR button.
        >
        > Here's my attempts, because I want to take care all html controls. I
        > think I need to test
        > if the control is submit button, regular button. But I don't know
        > what I should do on drop down box?
        >
        > function clearForm()
        > { var i=0;
        > for (i=0; i<InputForm.ele ments.length-1; i++)
        > { var obj = InputForm.eleme nts[i];
        > document.write( obj.type); //runtime error: object doesn't support
        > this property or method
        > if (obj.type != "submit" && obj.type != "button")
        > obj.value = "";
        > }
        > }
        >
        > any ideas?? thanks!![/color]


        as far as a select control, I have set the length to zero, thereby
        eliminating all the <option> items in the select control.

        Comment

        • Richard Cornford

          #5
          Re: Generic clearForm function when click CLEAR button

          bruce wrote:
          <snip>[color=blue]
          > function ClearFields ()
          > {
          > var iIdx,iCount,oOb j,sRes,sType
          > iCount=document .forms(0).lengt h[/color]

          Treating the - document.forms - collection as a function, calling it and
          passing an argument, is a Microsoftism and should not be expected to
          work on non-IE browsers (though it does on some as they are forced to
          put effort in to emulating IE's non-standard behaviour to accommodate
          script authors who aren't interested in other browsers).

          Normal (cross-browser) references to forms through the -
          document.forms - collection would use a bracket notation property
          accessor, and work on every browser that understands what a form is,
          including IE.

          iCount = document.forms[0].length;
          [color=blue]
          > for (iIdx=0;iIdx<iC ount;iIdx++)
          > {
          > oObj=document.f orms(0).item(iI dx)[/color]

          Re-resolving the reference to the form within a loop is inefficient, and
          treating the FORM element as implementing a HTMLCollection interface is
          relying on a non-specified coincidence that, although it is common in
          implementations , should not be relied upon.

          The controls within a from are traditionally, and by W3C HTML DOM
          specification, available as indexed members of the FORM's - elements -
          collection, and can be reliably accessed through that collection on
          every browser that understands what a form is:-

          var elsRef, oObj, sType;
          if((document.fo rms)&&
          ((elsRef = document.forms[0]))&&
          ((elsRef = elsRef.elements ))){

          for(var c = elsRef.length;c--;){
          oObj = elsRef[c];
          sType= (new String(oObj.typ e)).substring(0 ,5);
          if(sType=="sele c"){
          ...
          ... //etc.

          }
          }
          }

          Doing it this way also means that when the function is to be applied in
          a different context, to a form with a different index, there is only one
          place in the function where the index needs to be updated. Though a more
          general function would be passed the from reference as an argument.
          [color=blue]
          > sType=oObj.type .substring(0,5)
          > // alert("stype=" + oObj.type + " val=" + oObj.value)
          > if (sType=="selec" )
          > {
          > oObj.selectedIn dex=0
          > }[/color]

          If a SELECT element is select-multiple then setting its -
          selectedIndex - to zero will not necessarily have the desired result.
          [color=blue]
          > else
          > if (sType=="check" )
          > oObj.checked=fa lse
          > else
          > if (sType=="text")
          > oObj.value=""
          > else
          > if (sType=="radio" )
          > oObj.checked=tr ue
          > }[/color]

          What happened to TEXTAREA elements?
          [color=blue]
          > }[/color]


          Comment

          • Matt Kruse

            #6
            Re: Generic clearForm function when click CLEAR button

            Matt wrote:[color=blue]
            > I want to write a generic clearForm function to clear the form when
            > the user click CLEAR button.[/color]

            What does "clear" mean, exactly?
            In input controls, it's obvious - setting the value to ''

            But what about other controls:
            - Checkbox: Does it mean unchecking all checkboxes, or checking only
            boxes where value='' ?
            - Radio: You can't uncheck all radio buttons in a group, so which one
            stays selected?
            - Select: Do you select the option with value='' ? What if there are
            none?
            - Multi-select: Do you unselect all options, or select options with
            value='' ?
            - File: You can't set the value of these at all!

            In order to program to requirements, the requirements need to be clear :)

            --
            Matt Kruse
            Javascript Toolbox: http://www.mattkruse.com/javascript/


            Comment

            Working...