bodywise
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.fina lTotal1.va lue = document.form1.total.value ;
document.results_form.fina lTotal2.va lue = document.form2.total.value ;
document.results_form.fina lTotal3.va lue = document.form3.total.value ;
document.results_form.fina lTotal4.va lue = document.form4.total.value ;
document.results_form.fina lTotal5.va lue = document.form5.total.value ;
document.results_form.fina lTotal6.va lue = document.form6.total.value ;
document.results_form.fina lTotal7.va lue = document.form7.total.value ;
document.results_form.fina lTotal8.va lue = document.form8.total.value ;
document.results_form.fina lTotal9.va lue = document.form9.total.value ;
document.results_form.fina lTotal10.v alue = document.form10.total.valu e;
document.results_form.fina lTotal11.v alue = document.form11.total.valu e;
document.results_form.fina lTotal12.v alue = document.form12.total.valu e;
document.results_form.fina lTotal13.v alue = document.form13.total.valu e;
document.results_form.fina lTotal14.v alue = document.form14.total.valu e;
document.results_form.fina lTotal15.v alue = document.form15.total.valu e;
document.results_form.fina lTotal16.v alue = document.form16.total.valu e;
document.results_form.fina lTotal17.v alue = document.form17.total.valu e;
document.results_form.fina lTotal18.v alue = document.form18.total.valu e;
document.results_form.fina lTotal19.v alue = document.form19.total.valu e;
document.results_form.fina lTotal20.v alue = document.form20.total.valu e;
document.results_form.fina lTotal21.v alue = document.form21.total.valu e;
document.results_form.fina lTotal22.v alue = document.form22.total.valu e;
return document.results_form.fina lTotal1.va lue;
return document.results_form.fina lTotal2.va lue;
return document.results_form.fina lTotal3.va lue;
return document.results_form.fina lTotal4.va lue;
return document.results_form.fina lTotal5.va lue;
return document.results_form.fina lTotal6.va lue;
return document.results_form.fina lTotal7.va lue;
return document.results_form.fina lTotal8.va lue;
return document.results_form.fina lTotal9.va lue;
return document.results_form.fina lTotal10.v alue;
return document.results_form.fina lTotal11.v alue;
return document.results_form.fina lTotal12.v alue;
return document.results_form.fina lTotal13.v alue;
return document.results_form.fina lTotal14.v alue;
return document.results_form.fina lTotal15.v alue;
return document.results_form.fina lTotal16.v alue;
return document.results_form.fina lTotal17.v alue;
return document.results_form.fina lTotal18.v alue;
return document.results_form.fina lTotal19.v alue;
return document.results_form.fina lTotal20.v alue;
return document.results_form.fina lTotal21.v alue;
return document.results_form.fina lTotal22.v alue;
document.results_form.tota lscore.val ue = ????
return document.results_form.tota lscore.val ue;
}
The final document.results_form.tota lscore.val ue 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.
function final_results() {
// assigns values to the final input value <--- from each of 22 forms values
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
return document.results_form.fina
document.results_form.tota
return document.results_form.tota
}
The final document.results_form.tota
Tight, economical code always greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
a yeah, one bug I see :)
total+=obj.total.value;
should be:
total+=obj.total.value*1;
total+=obj.total.value;
should be:
total+=obj.total.value*1;
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.
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.
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.val ue;
document.results_form.elem ents['fina lTotal'+nu m].value = getForm;
grand_total += parseInt(getForm);
}
document.results_form.tota lscore.val ue = 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...
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]
document.results_form.elem
grand_total += parseInt(getForm);
}
document.results_form.tota
}
Are you calling the results from the "final_results()" function from inside another function, or why the "return" statements?
Anyway, hope that helps.
Regards...
ASKER
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['dest Form'].fin alTotal" + 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
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['dest
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 )
ASKER
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.
Thank you to all of the above for succinct and relevant answers.
Happy Turkey Day.
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_for
total+=obj.total.value;
return false;
}
}
else
{
document.results_form.tota
return true;
}
}
</script>