Link to home
Start Free TrialLog in
Avatar of bodywise
bodywiseFlag for United States of America

asked on

Make a for-loop from 22 document.form.value statements

I am close -- but no cigar.   I have a series of 22 forms that save input values (scores) and then pass to one "super" form that then sends to a response page.  How do I write these statements in a more compact for-loop?   Writing in Javascript document.write statements is cumbersome, but sometimes necesary since this will involve making these into either an array or variables .

function final_results() {
        
         // assigns values to the final input value <--- from each of 22 forms values
        document.results_form.finalTotal1.value = document.form1.total.value;
      document.results_form.finalTotal2.value = document.form2.total.value;
      document.results_form.finalTotal3.value = document.form3.total.value;
      document.results_form.finalTotal4.value = document.form4.total.value;
      document.results_form.finalTotal5.value = document.form5.total.value;
      document.results_form.finalTotal6.value = document.form6.total.value;
      document.results_form.finalTotal7.value = document.form7.total.value;
      document.results_form.finalTotal8.value = document.form8.total.value;
      document.results_form.finalTotal9.value = document.form9.total.value;
      document.results_form.finalTotal10.value = document.form10.total.value;
      document.results_form.finalTotal11.value = document.form11.total.value;
      document.results_form.finalTotal12.value = document.form12.total.value;
      document.results_form.finalTotal13.value = document.form13.total.value;
      document.results_form.finalTotal14.value = document.form14.total.value;
      document.results_form.finalTotal15.value = document.form15.total.value;
      document.results_form.finalTotal16.value = document.form16.total.value;
      document.results_form.finalTotal17.value = document.form17.total.value;
      document.results_form.finalTotal18.value = document.form18.total.value;
      document.results_form.finalTotal19.value = document.form19.total.value;
      document.results_form.finalTotal20.value = document.form20.total.value;
      document.results_form.finalTotal21.value = document.form21.total.value;
      document.results_form.finalTotal22.value = document.form22.total.value;
      
      return document.results_form.finalTotal1.value;    
      return document.results_form.finalTotal2.value;    
      return document.results_form.finalTotal3.value;    
      return document.results_form.finalTotal4.value;    
      return document.results_form.finalTotal5.value;    
      return document.results_form.finalTotal6.value;    
        return document.results_form.finalTotal7.value;    
      return document.results_form.finalTotal8.value;    
      return document.results_form.finalTotal9.value;    
      return document.results_form.finalTotal10.value;    
        return document.results_form.finalTotal11.value;    
      return document.results_form.finalTotal12.value;    
      return document.results_form.finalTotal13.value;    
      return document.results_form.finalTotal14.value;    
      return document.results_form.finalTotal15.value;    
      return document.results_form.finalTotal16.value;    
      return document.results_form.finalTotal17.value;    
      return document.results_form.finalTotal18.value;    
      return document.results_form.finalTotal19.value;    
      return document.results_form.finalTotal20.value;  
      return document.results_form.finalTotal21.value;    
      return document.results_form.finalTotal22.value;    
      
      document.results_form.totalscore.value = ????
      return document.results_form.totalscore.value;
      }

The final document.results_form.totalscore.value is a summation value that could be an increment statement or a final addition of all of the above.

Tight, economical code always greatly appreciated.
Avatar of devic
devic
Flag of Germany image

I hope I did without errors :)

in every form add keyword "this" on onsubmit attribute

like this:
<form onsubmit="return final_results(this)">


and the function:

============================
<script>
function final_results(obj)
{
      // assigns values from the 22 forms into an input of the final super form
      if(obj.name!="totalscore")
      {
            var total=0;
            for(var i=1;i<23;i++)
            {
                  eval("document.results_form.finalTotal"+i+".value = obj.total.value;")
                  total+=obj.total.value;
                  return false;
            }
      }
      else
      {
               document.results_form.totalscore.value = total;
            return true;
      }
}

</script>
ASKER CERTIFIED SOLUTION
Avatar of _aaron_
_aaron_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
a yeah, one bug I see :)

total+=obj.total.value;

should be:

total+=obj.total.value*1;
Avatar of bodywise

ASKER

Again ... thanks for such a speedy response.  I need to work through this and get back to both of you in a few hours (I am still at work)  ...  I  think this is right.  

The partial answer is, yes, this is supposed to be a one time call at the end of complete of the 22 forms.   So each of the 22 forms needs to call up an "upTotal"  function that totals the results.  The superform just inputs these total values all at once to post to the response page.

Avatar of _aaron_
_aaron_

The example given provides the total in the "FINAL" input box, there is no need call another 'upTotal' function; anyway, see how it goes ;-)
Hi bodywise,

Here is the code you have, (basically), in compact form. I do not understand why you need to "return" all of those values, or where you are returning them to. If you can show me more of your code, I could maybe understand.

Here is the script:

function final_results(){
     for(i=0;i<22;i++){
         num = i+1;
         var num = num.toString();
         getForm = document.forms['form'+num].total.value;
         document.results_form.elements['finalTotal'+num].value = getForm;
         grand_total += parseInt(getForm);
     }
     document.results_form.totalscore.value = grand_total;
}

Are you calling the results from the "final_results()" function from inside another function, or why the "return" statements?

Anyway, hope that helps.

Regards...
Gee.  Thanks to all of you.   This has been a very short and sweet interchange.  I went with the simplest and most effective solution from aaron, although I think the other suggestions from all of you would work as well.  Eadh teaches me just a slight variation.  obj.total.value is another very clever convention that I have not used.

Here is what I learned that I did not quite understand.

1. form can be named as forms['form'+i]    most clever and most compact

2. the part eval("document.forms['destForm'].finalTotal" + i ).value  is what I was looking for.  I could not figure out how to phrase this and the end  == finalTotal" + i ).value ==  was just not apparent to me.  It works!

3. the increment counter works just right -- I think -- although it is not the absolute critical value.  the 22 total scores are the important variables that are passed to the response page

4. the <body onload> statement is probably critical to setting up the "re-evaluation" process

5. I had to change the counter from 0 to 1 since my forms are named as form1, form2 etc and not form0, form1 ...  The 0 array system still confuses me greatly.   I seem to remember that Pascal allows you to change array counting from 0 to 1.  I don't think you have that option in JS so I just put dummmy text in the first array element.

hope to encounter you again with any remianing problems.bodywise

here is the rather hidden page that you may need to sign-in to view.

http://www.antiagingnow.com/secure/test_forms/health_survey.php
Great, happy to see that u learnt so much... I am still constantly learning new and improved ways of coding in JScript - its a fairly amazing language ( well I think anyway )

Indeed.  I have programmed in TurboPascal, PHP, ASP, a tiny bit of Fortran, Basic, and here comes along Javascript.  Yes, it is a rather amazing scripting language.

Thank you to all of the above for succinct and relevant answers.

Happy Turkey Day.