8 Replies Latest reply on May 12, 2006 11:12 AM by suchil00

    Copy form field values

    suchil00
      I am running a query to create a dynamic form. I want to copy the values in one field to another. So far I have this.

      ### JAvascript part###
      <script language="JavaScript">
      var bCopy = true;
      function doCopy() {
      for(i=1; i<100; i++) {
      document.getElementById('rec'+i).value = document.getElementById('ord'+i).value;
      }
      }

      function doClear() {
      for(i=1; i<100; i++) {
      document.getElementById('rec'+i).value = 0;
      }
      }

      function doAction() {
      if(bCopy) {
      doCopy();
      bCopy = false;
      } else {
      doClear();
      bCopy = true;
      }
      }

      </script>

      ### CFM part###

      <td><b>Qty</b></td>
      <td><b><a href=# onClick="doAction();">Rec</a></b></td>

      <cfoutput query="Details">
      <td><input class="input_text" type="hidden" id="ord#CurrentRow#" name="OLD_QTY_ORDERED#CurrentRow#" value="#QTY_ORDERED#"></td>
      <td><cfinput type="text" id="rec#CurrentRow#" name="QTY_RECEIVED#CurrentRow#" value="#NumberFormat(QTY_RECEIVED)#"></td>
      ###end###

      I want to toggle the value of the second field between the values of the first field and '0' by clicking on the column header.

      2 questions
      the first part works, but it does not toggle back to 0.
      how to pass the recordcount into the loop
        • 1. Copy form field values
          The ScareCrow Level 1
          You should make this independant of the cf query, you can do this by looping through the document elements

          Note:
          replace "formName" with the name of your form

          <script language="JavaScript">
          var bCopy = true;
          function doCopy() {
          // loop through the document elements
          for(i=0; i<document.formName.elements.length; i++) {
          // find the element name (id)
          elementName = document.formName.elements .name;
          // extract the appended index of the element
          elementIndex = elementName.substring(4,elementName.length);
          if(elementName.indexOf('rec')){
          document.formName.elements
          .value = document.getElementById('ord' + elementIndex).value;
          }
          }
          function doClear() {
          for(i=0; i<document.formName.elements.length; i++) {
          elementName = document.formName.elements .name;
          if(elementName.indexOf('rec')){
          document.formName.elements
          .value = 0;
          }
          }
          </script>
          Note this code is untested.

          Ken
          • 2. Re: Copy form field values
            Dan Bracuk Level 5
            Have you thought about not copying the values at all? Just use the existing fields twice when you process the form.
            • 3. Re: Copy form field values
              suchil00 Level 1
              Thanks Scare Crow - this part definitely helped
              for(i=0; i<document.formName.elements.length; i++)

              I am still not getting it to toggle back to '0'
              • 4. Re: Copy form field values
                suchil00 Level 1
                Dan - do you mean do the query again and reload the page after checking for a variable and then fill in the values using CF?
                • 5. Re: Copy form field values
                  The ScareCrow Level 1
                  The code I posted has some errors in it.
                  I also noticed that you have a different "name" and "id" value for the form fields !!

                  But here is a working example

                  Ken

                  • 6. Re: Copy form field values
                    suchil00 Level 1
                    Used the same ID's and NAMES and now with your new code, it works. Thanks ScareCrow!
                    • 7. Re: Copy form field values
                      Dan Bracuk Level 5
                      quote:

                      Originally posted by: freezer9
                      Dan - do you mean do the query again and reload the page after checking for a variable and then fill in the values using CF?

                      This is what I mean. You start by saying this:
                      I am running a query to create a dynamic form. I want to copy the values in one field to another. So far I have this.

                      For the sake of simplicity let's say there are only two fields, f1 and f2. You have a value in f1 and you want f2 to either have the same value or be equal to 0. At least that's how I understand it.

                      You are currently using a javascript onclick event to control the value of f2. This is hard, hence your difficulties. An easier way is to get rid of f2. Add a check box or radio button for the user input.

                      When you submit your form, you want two variables. You already have the first one, form.f1. The second one either has to equal form.f1 or has to be 0. Assign that value based on what the user did with the check box or radio button.
                      • 8. Re: Copy form field values
                        suchil00 Level 1
                        Thanks Dan - I understand it now. That would work except that it is not an "all or nothing" situation always. It is just like if you are looking at your junkmail folder - you want to delete all the 50 messages that you see in that page, expect, may be 1 or 2 - you, would then click on the checkbox, and then 'un-select' only the ones that you don't want to delete. I am trying to accomplish something like that without using the checkbox on each row. So here with the solution that SCARECROW gave, I am able to let the user automatically fill in the values for most of the rows, and then 'zero' out some manually. Sorry for not explaining in detail.
                        Thanks guys!