Click to See Complete Forum and Search --> : computing values from a form optimized


collins
02-12-2003, 10:31 AM
I have a script that I think can be more compact, but I'm just learing and would like some gudiance if possible.

What I am trying to do

I have a function that looks like this: form.gradepoints1.value = form.grade1.value*form.hours1.value
but it needs to appear 40 times. Is there a way to use a loop instead of 40 versions of this same string with the value 1-40:
form.gradepoints1.value = form.grade1.value*form.hours1.value
form.gradepoints2.value = form.grade2.value*form.hours2.value
etc...
form.gradepoints40.value = form.grade40.value*form.hours40.value

I tried using:
var x;
for (x = 1; x < 41; x++)
{
form.gradepoints[x].value = form.grade[x].value*form.hours[x].value
}

But nothing computed. Any suggestions?

khalidali63
02-12-2003, 10:51 AM
there is eval function that you can use,but my choice will be code below.
Just add the text fields as many you want and the l10 line function will take care of it.

cheers

Khalid


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"></meta>
<meta name="Author" content="Khalid Ali"></meta>
<title>Untitled</title>
<link type="text/css" rel="stylesheet" href="http://68.145.35.86/skills/javascripts/style/js_tutorials_style.css">
<style type="text/css">

</style>
<script type="text/javascript">
function Process(){
var tGrades = document.form1.grades;
var tHours = document.form1.hours;
var tGP = document.form1.gradepoints
var len = tGrades.length;
for(x=0;x<len;x++){
tGP[x].value = parseInt(tGrades[x].value)*parseInt(tHours[x].value);
}
}

</script>
</head>
<body>
<form name="form1" action="">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>Grade:<input type="Text" name="grades"></input></td>
<td>Hours<input type="Text" name="hours"></input></td>
<td>Grade Point:<input type="Text" name="gradepoints"></input></td>
</tr>
<tr>
<td>Grade:<input type="Text" name="grades"></input></td>
<td>Hours<input type="Text" name="hours"></input></td>
<td>Grade Point:<input type="Text" name="gradepoints"></input></td>
</tr>
<tr>
<td>Grade:<input type="Text" name="grades"></input></td>
<td>Hours<input type="Text" name="hours"></input></td>
<td>Grade Point:<input type="Text" name="gradepoints"></input></td>
</tr>
</table><br>
<input type="Button" value="process" onclick="Process();"></input>
</form>
</body>
</html>

collins
02-12-2003, 03:00 PM
Thanks Khalid,

That did the trick for the long list.

If possible, can you give me a little advice on how to add the column of numbers (if it is possible)

I tried modifying the code with:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"></meta>
<meta name="Author" content="Khalid Ali"></meta>
<title>Untitled</title>

<script type="text/javascript">
function Process(){
var tGrades = document.form1.grades;
var tHours = document.form1.hours;
var tGP = document.form1.gradepoints
var tGPttl = document.form1.gradetotals
var tHoursttl = document.form1.hourstotal
var len = tGrades.length;
for(x=0;x<len;x++){
tGP[x].value = parseInt(tGrades[x].value)*parseInt(tHours[x].value);
tHoursttl.value = tHoursttl.value + parseInt(tHours[x].value);
tGPttl.value = tGPttl.value + parseInt(tGrades[x].value);
}
}

</script>

</head>
<body>
<form name="form1" action="">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td>Grade:<input type="Text" name="grades"></input></td>
<td>Hours<input type="Text" name="hours"></input></td>
<td>Grade Point:<input type="Text" name="gradepoints"></input></td>
</tr>
<tr>
<td>Grade:<input type="Text" name="grades"></input></td>
<td>Hours<input type="Text" name="hours"></input></td>
<td>Grade Point:<input type="Text" name="gradepoints"></input></td>
</tr>
<tr>
<td>Grade:<input type="Text" name="grades"></input></td>
<td>Hours<input type="Text" name="hours"></input></td>
<td>Grade Point:<input type="Text" name="gradepoints"></input></td>
</tr>
<tr>
<td><input type="text" name="hourstotals" readonly size="3"></td>
<td><input type="text" name="gradetotals" readonly size="6"></td>
</tr>
</table><br>
<input type="Button" value="process" onclick="Process();"></input>
</form>
</body>
</html>

I keep getting an undefined is not an object error.