Get form field value using an offset in a Function

I have a Form created using Perl that has a table with a row for each invoice in a large table. I display all the rows in a table and allow the user to edit some info for each record:

for ($item_cntr = 0; $cntr < $record_cnt; ++$item_cntr)
{ print qq(<tr>
<td><input type="text" name="c_freight_$item_cntr" value="$i_freight[$item_cntr]" onBlur="linetotal($item_cntr)" ></td>
<td><input type="text" name="c_miscamt_$item_cntr" value="$i_miscamt[$item_cntr]" ></td>
<td><input type="text" name="c_price_$item_cntr" value="$i_price[$item_cntr]" ></td>
</tr>);
}

I want to create a Function that is called when they change the Freight value and that causes the rows total Price (freight + miscamt = price) to be calculated again (without hitting a Submit button on the Form).

I created a Function:

function linetotal(whatline) {
var fixed_freight = "document.editform.c_freight_" + whatline;
var fixed_misc = "document.editform.c_miscamt_" + whatline;
var linetotal = parseFloat(fixed_freight.value) + parseFloat(fixed_misc.value);

document.editform.c_price_whatline.value = linetotal; } }

So, if the first row in the table is 0, then the Function is passed "0" and should get the value of the form fields:
'c_freight_0' and 'c_miscamt_0' and set 'c_price_0' to a new value in the form.

The fixed_freight is showing that the string it has is correct, but it does not get the value from the form. Any ideas?
sverneAsked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
Where is your <form> tag?
Put it near the body tag:
<body>
<form>
<table>
...
</table>
</form>
</body>
</html>
0
 
ZvonkoSystems architectCommented:
You create String objects in JavaScript and read the String.value property. That does not work.

Change your onBlur event handler to this: onBlur="linetotal(this)"

Then your script goes like this:
function linetotal(theField) {
  var whatline = theField.name.replace(/\D/g,"");
  var theForm = theField.form;
  var fixed_freight = theField.value;
  var fixed_misc = theForm["c_miscamt_" + whatline].value;
  var total = parseFloat(fixed_freight) + parseFloat(fixed_misc);
  theForm["c_price_"+whatline].value = total.toFixed(2);
}


0
 
sverneAuthor Commented:
Error on the fixed_misc line
<html><head>
<script type="text/javascript" language="JavaScript">
function linetotal(theField) {
  var whatline = theField.name.replace(/\D/g,"");
  var theForm = theField.form;
  var fixed_freight = theField.value;
  var fixed_misc = theForm["c_miscamt_" + whatline].value;
  var total = parseFloat(fixed_freight) + parseFloat(fixed_misc);
  theForm["c_price_"+whatline].value = total.toFixed(2); 
}
</script></head><body>
 
<table>
<tr>
<td><input type="text" name="c_invoice_0"  value='123'></td>        
<td><input type="text" name="c_freight_0"  value='1.00' onBlur="linetotal(this)" ></td>        
<td><input type="text" name="c_miscamt_0"     value='2.00'></td>        
<td><input type="text" name="c_price_0"     value='3.00'></td>        
</tr>
<tr>
<td><input type="text" name="c_invoice_1"  value='124'></td>        
<td><input type="text" name="c_freight_1"  value='17.00' onBlur="linetotal(this)" ></td>        
<td><input type="text" name="c_miscamt_1"     value='20.00'></td>        
<td><input type="text" name="c_price_1"     value='37.00'></td>        
</tr>
<tr>
<td><input type="text" name="c_invoice_2"  value='125'></td>        
<td><input type="text" name="c_freight_2"  value='5.00' onBlur="linetotal(this)" > </td>        
<td><input type="text" name="c_miscamt_2"     value='6.00'></td>        
<td><input type="text" name="c_price_2"     value='11.00'></td>        
</tr>
</table></body></html>

Open in new window

0
 
sverneAuthor Commented:
Problem resolved and the code is attached below
<html><head>
<script type="text/javascript" language="JavaScript">
function linetotal(theField) {
  var whatline = theField.name.replace(/\D/g,"");
  var theForm = theField.form;
  var fixed_freight = theField.value;
  var fixed_misc = theForm["c_miscamt_" + whatline].value;
  var total = parseFloat(fixed_freight) + parseFloat(fixed_misc);
  theForm["c_price_"+whatline].value = total.toFixed(2); 
}
</script></head><body>
<form method="POST" action="rb_return_create_test.pl" name="editform">
 
<table>
<tr>
<td><input type="text" name="c_invoice_0"  value='123'></td>        
<td><input type="text" name="c_freight_0"  value='1.00' onBlur="linetotal(this)" ></td>        
<td><input type="text" name="c_miscamt_0"     value='2.00'></td>        
<td><input type="text" name="c_price_0"     value='3.00'></td>        
</tr>
<tr>
<td><input type="text" name="c_invoice_1"  value='124'></td>        
<td><input type="text" name="c_freight_1"  value='17.00' onBlur="linetotal(this)" ></td>        
<td><input type="text" name="c_miscamt_1"     value='20.00'></td>        
<td><input type="text" name="c_price_1"     value='37.00'></td>        
</tr>
<tr>
<td><input type="text" name="c_invoice_2"  value='125'></td>        
<td><input type="text" name="c_freight_2"  value='5.00' onBlur="linetotal(this)" > </td>        
<td><input type="text" name="c_miscamt_2"     value='6.00'></td>        
<td><input type="text" name="c_price_2"     value='11.00'></td>        
</tr>
</table></form></body></html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.