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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accumulating fields value 1

Status
Not open for further replies.

REK2

Programmer
Apr 30, 2003
22
CA
Hi all,
I have this script here;
*************************************
<HTML>
<HEAD>
<TITLE></TITLE>
<script language=&quot;JavaScript&quot;>
__uid = 0;
template = [{&quot;width&quot;:&quot;&quot;,&quot;prefix&quot;:&quot;Descr&quot;,&quot;size&quot;:35},
{&quot;width&quot;:&quot;&quot;,&quot;prefix&quot;:&quot;Amount&quot;,&quot;size&quot;:5}];
doc = document;
function addRowTo(id) {
var tbl = doc.getElementById(id);

// create a new row
var newrow = doc.createElement(&quot;TR&quot;);

var newcol , newinput;
for (var i in template) {
newcol = doc.createElement(&quot;TD&quot;);
newcol.width = template[&quot;width&quot;];

newinput = doc.createElement(&quot;INPUT&quot;);
newinput.name = template[&quot;prefix&quot;]+__uid;
newinput.size = template[&quot;size&quot;];

newcol.appendChild(newinput);
newrow.appendChild(newcol);
}

tbl.appendChild(newrow);
newrow.cells[template.length-1].firstChild.onblur=function(){addRowTo('tbl1body')};
newrow.cells[0].firstChild.focus();
__uid++;
}
</script>
</HEAD>

<body onLoad=&quot;addRowTo('tbl1body')&quot;>
<form name=&quot;mainform&quot; method=&quot;POST&quot; action=&quot;&quot;>
<table id=&quot;tbl1&quot;>
<tbody id=&quot;tbl1body&quot;>
<tr align=right><td><B>Total :</B><INPUT TYPE=&quot;text&quot; size=8 NAME=&quot;PortTotal&quot; ID = &quot;PortTotal&quot; readonly value=&quot;0&quot; style=&quot;background-color:#FFFFFF;border-style:solid;border-width:0;border-color:;&quot;> </td></tr>

<tr><td><B>Description</B></td><td><B>Amount</B></td></tr>
</tbody>
</table>
<!-- <button onclick=&quot;addRowTo('tbl1body');&quot;>Add Row >></button> -->
<input id=&quot;submit&quot; type=&quot;submit&quot; name=&quot;save&quot; value=&quot;Save&quot;>
</form>
</BODY>
</HTML>
*************************************************

What it does is that I enter a description and an amount and when &quot;tab&quot; another row is added in order to repeat the process.

Now what I am trying to acheive without success is that everytime I hit tab and that a new row is added I need to cumulate the values entered in the second field and display it in the field call &quot;total&quot;. So if in the first row I enter 20 in the amount field when a new row is added the value 20 must be displayed in the total field, if I enter 30 in the second row than when hit tab the value 50 must appear in the total field, and so on...

I really do not know how to acheive this and I've tried severals things...

I know I also have a field validation to ensure that it's only numeric that is entered in the field value, but this is not my problem currently.. so let's simply assume that we are always entering numbers in there. I will put my validation after.

Any help would be more than greatly appreciated.
Thanks alot
Greetings
 
See this thread for ideas or hints: thread216-557502

Maybe part of this can help you out:

Code:
// JavaScript function
function sum_boxes(callfrom)
{
 var e = document.mainform.length;
 var tot=0;
 var i;
 for (i=0; i < e; i++)
 {
  // scan all form elements to find ones we need
  var n=document.mainform.elements[i].name;
  var v=document.mainform.elements[i].value;
  if (n.substr(0,6) == &quot;prefix&quot; && v.length != 0)
  {
   if (0 < v)
   {
    // add another value to tot variable
    // second parameter forces decimal base 10 evaluation
    tot += parseInt(v,10)
   }
  }
 }
 document.mainform.PortTotal.value=tot
}

Remember, you have to keep the exact capitalization in JavaScript, so &quot;parseInt()&quot; will work but not &quot;parseint()&quot;.

For each INPUT textbox you can use <INPUT ... ONBLUR=&quot;return sum_boxes('textbox')&quot;>. I added the parameter only in case you wanted to test differently at the entrybox level versus the submit level.
 
Hi dbMark,
Thanks alot for your input, I've already had seen the thread you've post me and was not able to work something out because my fields on the form are created dynamically... but am pretty sure that I would be able to catch the idea and apply it to my needs.

I'll have a try in the course of the evening and let you know the issue.

Thanks again for you input
Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top