Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prob with functions and form calls eg. form.see[0].value

Status
Not open for further replies.

masey

Technical User
Mar 18, 2004
1
GB
Hi friends,
I have a form with textboxes that gets passed to a Javascript function inthe head of my document when a button is clicked.
Problem: Textboxes are automatically named in a for loop in tthe form so to know which one to call I must also have a for loop in my Javascript function.
So I have:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function calcul(form)
{
//calculates total for 3 boxes in a row of a table
//and inserts the total into a fourth box in the row.
//i represents number of rows there are in the table.
//Table initially constructed in the form that is
//passed in so all variables for the textboxes are
//known.

for(var i=0;i<form.nombre.value; i++)
{
var c13=&quot;text&quot;+i+&quot;13&quot;;//name of a textbox,so get
// text013
var c17=&quot;text&quot;+i+&quot;17&quot;;
var c21=&quot;text&quot;+i+&quot;21&quot;;
document.write(form.c13.value);//prob:error here
//system cant doesn't replace c13 with text013
//so that the value of that textbox is received..
//i.e. I get &quot;form.c13.value=undefined&quot;,when I need
//form.text013.value= some number, so rest below
//is not executed, same prob below

var r = form.c13.value + form.c17.value+
form.c21.value;
document.write(r);
var cr =&quot;text&quot;+i+&quot;22&quot;;
form.cr.value =r;
}

var CombTotal=0;
for(var d=0;d<form.nomb.value;d++)//set total for
// each row
{ var cr =&quot;text&quot;+d+&quot;22&quot;;
CombTotal+=form.cr.value;
}

form.CoutTotale.value =CombTotal;
}
</SCRIPT>
 
Assuming you have passed the name of the form as the parameter to the function, try this -
Code:
var c13= eval(&quot;form.text&quot; + i + &quot;13.value&quot;);
document.write(c13);

The eval() function figures out what object you have created a name for in the string. Then give that object a new name that is easier to type. Do this for all three fields, or however many you have then -
Code:
var r = c13 + c17 + c21;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top