?
Solved

Problems with sum in javascript

Posted on 2007-12-02
21
Medium Priority
?
360 Views
Last Modified: 2012-06-21
Can't figure out to make a sum in java based on value of other form field.

function calctotal() {
  var txt, i=0, soma=0;
   if (document.getElementById("btwa"+i) == "21") {
  while (txt = document.getElementById("subtot"+i)) {
    soma += parseFloat(txt.value);
    i++;
  }
  }
  document.banza.totalm21.value = soma;
}
0
Comment
Question by:frippel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
21 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 20394235
What should this line do:
while (txt = document.getElementById("subtot"+i)) {

Perhaps you wanted this:
while (txt == document.getElementById("subtot"+i)) {


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20394245
Check this:
function calctotal() { 
  var txt, i=0, soma=0;
  if (document.getElementById("btwa"+i) == "21") {
    while (txt = document.getElementById("subtot"+i)) { 
      var subTot = parseFloat(txt.value); 
      if(!isNaN(subTot)) soma += subTot; 
      i++; 
    } 
  }
  document.banza.totalm21.value = soma; 
}

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20394256
Sorry, that cannot work either because of this:
if (document.getElementById("btwa"+i) == "21") {

You cannot compare an Element with an String and get back True
Perhaps you wanted to do this:

function calctotal() { 
  var txt, i=0, soma=0;
  if (document.getElementById("btwa0").value == "21") {
    while (txt = document.getElementById("subtot"+i)) { 
      var subTot = parseFloat(txt.value); 
      if(!isNaN(subTot)) soma += subTot; 
      i++; 
    } 
  }
  document.banza.totalm21.value = soma; 
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:frippel
ID: 20394274
basically what i want to do is  this (sorry different approach)
i want the function to calculate the sum of certain fields if another field equals 21
dynamic table so
i tried the following, but doesn't work either

function calctotal() {
 var soma = 0

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
if(document.getElementById('btwa'+i)=='21')
soma = soma + (document.getElementById('subtot'+i).value*1);
  }
  }
  document.banza.totalm21.value = soma;
}



0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20394306
If you do not read my comments then I cannot help you.
You again compare a field to a string. You can only compare a field value to a string.
0
 

Author Comment

by:frippel
ID: 20394315
sorry,
Forgot to add .value, new at this...
function calctotal() {
  var soma = 0

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
if(document.getElementById('btwa'+i).value=='21')
soma = soma + (document.getElementById('subtot'+i).value*1);
  }
  }
  document.banza.totalm21.value = soma;
}
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20394330
So if field btwa17 value is 21 you want all following fields with prefix subtot be summed up?
It does not sound right...

Show your html source. If it is dynamic then one page source as seen in browser as example.
0
 

Author Comment

by:frippel
ID: 20394368
code is way to long since there are many dif criteria and fields but i'll try to show you the table
uses inerHTML with different var...
but i'll try to show you the part i'm having problems with

|  subtot1 |    | btwa1 |
| subtot2 |    | btwa2 |
...
| subtot99 |    | btwa99 |

the 1,2 are dynamicaly generated
subtot is a value the user fills in
btwa is a selection field  and can either be 6 or 21

I would like to do a calcualtion of all the subtot, where the user set 21 to the btwa field...

hope this helps explain my situation better?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20394392
That goes like this:

function calctotal() { 
  var soma=0;
  for(var i=0;txt=document.getElementById("btwa"+i);i++){
    if(txt.value==21) { 
      var subTot = document.getElementById('subtot'+i).value * 1;
      if(!isNaN(subTot)) soma = soma + subTot;
    } 
  }
  document.banza.totalm21.value = soma; 
} 

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 400 total points
ID: 20394401
And for older browser this:
function calctotal() { 
  var soma=0;
  var theForm = document.forms.banza;
  for(var i=0;txt=theForm["btwa"+i];i++){
    if(txt.value==21) { 
      var subTot = theForm['subtot'+i].value * 1;
      if(!isNaN(subTot)) soma = soma + subTot;
    } 
  }
  theForm.totalm21.value = soma; 
} 

Open in new window

0
 

Author Comment

by:frippel
ID: 20394416
still somewhere a problem, no errors anymore...but result gives me 0
0
 

Author Comment

by:frippel
ID: 20398612
Really tried a lot of different combinations but to no avail, only get 0 for resul for this script...

Somewhere i'm missing something but what?
It's this element that makes it 0 but i dont knowwhat it has to change to.
txt=theForm["btwa"+i]

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20399092
Is you page accessible online?
0
 

Author Comment

by:frippel
ID: 20399291
it's an intranet page but i'll copy some of the source code is that can help...

the CalculateTotal works fine which is somewhat based on the same idea.

hope this helps

<script language="javascript">	 
row_no=1; 
 
function addRow(tbl,row){ 
        //so that user can only add 3 rows 
        if(row_no<=500){ 
		var bolly= "box" + row_no; 
        var oms1= "desa" + row_no;
		var oms2= "desb" + row_no;
		var oms2z= "desbz" + row_no;
		var oms3= "desc" + row_no;
		var oms3z= "descz" + row_no;
		var getal1= "getala" + row_no;
		var getal2= "getalb" + row_no;
		var getal3= "getalc" + row_no;
		var getal4= "getald" + row_no;
		var getal5= "getale" + row_no;
		var btwset= "btwa" + row_no;
		var subtota= "subtot" + row_no;
		var typelijn= "typelijn" + row_no;
		var textbolly='<input name=' + bolly + ' type="checkbox" type="type1z"  value="" onClick="if(document.banza.' + bolly + '.checked) {document.banza.' + bolly + '.value=document.banza.' + subtota +'.value;}else{document.banza.' + bolly + '.value=0;}">';
		if(document.banza.whatkind.value == "dsvint"){
		var textoms1='<input type="text" class="type1z" name=' + oms1 + ' size="10" maxlength="10" value="<%=strDate%>">';}
		if(document.banza.whatkind.value == "dsvext"){
		var textoms1='<input type="text" class="type1z" name=' + oms1 + ' size="10" maxlength="10" value="week x">';}
		if(document.banza.whatkind.value == "dsvint"){
		var textoms2='<input type="text" name=' + oms2 + ' class="type1z" id=' + oms2 + ' size="40" maxlength="100" value="00 UREN 00 MIN EN 000 KM">';}
		if(document.banza.whatkind.value == "dsvext"){
		var textoms2='<input type="text" name=' + oms2 + ' class="type1z" id=' + oms2 + ' size="40" maxlength="100">';}
		if(document.banza.whatkind.value == "dsvint"){
		var textoms3='<input type="text" name=' + oms3 + ' class="type1z" id=' + oms3 + ' size="40" maxlength="100" value="TOER 00-1111">';}
		if(document.banza.whatkind.value == "dsvext"){
		var textoms3='<input type="text" name=' + oms3 + ' class="type1z" id=' + oms3 + ' size="40" maxlength="100" value="">';}
		
		var textsub='<input type="text" name=' + subtota + ' class="type2z" id=' + subtota + ' size="10" maxlength="30" value="" onchange="document.banza.' + bolly + '.checked=false; document.banza.' + bolly + '.value =0;">';
		var textbtw='<select name=' + btwset + ' class="type1z" id=' + btwset +'><OPTION VALUE="21">21</OPTION><OPTION VALUE="6">6</OPTION></select>';	
		var remove= '<a href="javascript:void(null)" onclick="removeRow(\''+ tbl +'\',\'' + row_no + '\')"/><img src="Images/del.gif" border="0" align="absmiddle"/></a>';    
        			
        var tbl = document.getElementById(tbl);//to identify the table in which the row will get insert 
        var rowIndex = document.getElementById(row).value;//to identify the row after which the row will be inserted 
        try { 
            var newRow = tbl.insertRow(row_no);//creation of new row 
            var newCell = newRow.insertCell(0);//first  cell in the row  
            var newCell = newRow.insertCell(1);//second cell in the row 
			if(document.banza.whatkind.value == "dsvint")
			{
            newCell.innerHTML = "<table width='600' border='0' cellspacing='0' cellpadding='0'><tr><td valign='top'>"+ remove + "</td><td valign='top'><p class='contacttable'>"+ textbolly + "</p></td><td valign='top'><p class='contacttable'>" + textoms1 + "</p></td><td valign='top'><p class='contacttable'>" + textoms2 + "<br>" + textoms3 + "</p></td><td valign='top'><p class='contacttable'>" + textsub + "¬</p></td><td valign='top'><p class='contacttable'>" + textbtw + "</p></td></tr><tr><td colspan='6'><hr style='border=thin;color=#666666'/></td></tr></table>";
			}
			if(document.banza.whatkind.value == "dsvext")
			{
            newCell.innerHTML = "<table width='600' border='0' cellspacing='0' cellpadding='0'><tr><td valign='top'>"+ remove + "</td><td valign='top'><p class='contacttable'>"+ textbolly + "</p></td><td valign='top'><p class='contacttable'>" + textoms1 + "</p></td><td valign='top'><p class='contacttable'>" + textoms2 + "<br>" + textoms3 + "</p></td><td valign='top'><p class='contacttable'>" + textsub + "¬</p></td><td valign='top'><p class='contacttable'>" + textbtw + "</p></td></tr><tr><td colspan='6'><hr style='border=thin;color=#666666'/></td></tr></table>"<!--
			data = [<%strSQL = "SELECT omschrijving FROM typelijn WHERE soort='dsvext'"%><%Recordset2.Open strSQL, Conn%><%Do While Not Recordset2.EOF %><%omschrijving = Recordset2("omschrijving")%>'<%=omschrijving%>',<% Recordset2.movenext %><% loop %> <% Recordset2.close %>'--> Maak een keuze'].sort();AutoComplete_Create('desb' + row_no , data);// -->
			;
			}
			banza.factor.value=row_no;
            row_no++;
        } catch (ex) { 
            alert(ex); //if exception occurs 
        } 
            
    } 
    if(row_no>500)//if the row contain 3 textbox, the add button will disapper 
    { 
        document.getElementById("add").style.display="none"; 
    }                        
} 
function removeRow(tbl,num) 
{ 
    var table = document.getElementById(tbl);//adentification of table 
    try { 
        row_no--; 
        table.deleteRow(num);//deletion of the clicked row 
		banza.factor.value=row_no-1;
    } catch (ex) { 
        alert(ex); 
    } 
    
    if(row_no<=500)//if row is less than 3 then the button will again appear to add row 
    { 
            document.getElementById("add").style.display="block"; 
    }    
} 
</script>
<script language="JavaScript" type="text/javascript">
function CalculateTotal(frm) {
    var order_total = 0
 
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
 
        // Get the current field
        form_field = frm.elements[i]
 
        // Get the field's name
        form_name = form_field.name
 
        // Is it a "product" field?
		if (form_name.substring(0,3) == "box") {
		
		
            // Get the quantity
            item_quantity =form_field.value
			item_quantity=item_quantity.replace(/,/g,".")
        item_price = 1
		
                order_total += item_quantity * item_price
        }
		
		
	
		
    }
 
    // Display the total rounded to two decimal places
	frm.totaalz21.value= Math.round(order_total*100)/100;
}
 
function calctotal() { 
  var soma=0;
  var theForm = document.forms.banza;
  for(var i=0;txt=theForm['btwa'+i];i++){
    if(txt.value==21) { 
      var subTot = theForm['subtot'+i].value * 1;
      if(!isNaN(subTot)) soma = soma + subTot;
    } 
  }
  theForm.totalm.value = soma; 
} 
</script>
 
<form name="banza" id="banza">
...
<table width="600" border="0" cellspacing="0" id="mytable">
<tr id="myrow"></tr></table>
<div id="add"><p class="contacttablew">&raquo;&nbsp;&nbsp;+<a class="table" href="javascript:void(null)" onClick="document.banza.whatkind.value='dsvint'; addRow('mytable','myrow')">DSVint</a>&nbsp;&nbsp;+<a class="table" href="javascript:void(null)" onClick="document.banza.whatkind.value='dsvext'; addRow('mytable','myrow')">DSVext</a></p></div>
<table width="600" border="0" cellspacing="0">
<tr><td></td><td><p class="contacttable"><br><input type="text" name="totaalz21"><input onClick="CalculateTotal(this.form); if(document.banza.totaalz21.value.indexOf('.') >= 0) {
document.banza.totaalz21.value = document.banza.totaalz21.value.replace(/\./g,',');}" name="OK" value="bereken" type="button"><br>
<input type="text" name="totalm"><input onClick="calctotal(this.form);" name="OK" value="bereken" type="button">

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20399480
THe upper lines do not realy help because they are mixed with the ASP web server scripting code.
Can you please post one complette html page as seen in browser page html source?

0
 

Author Comment

by:frippel
ID: 20399566
There you ask me something. It's all based on asp pges with inludes...

over 2000 line codes, but it really should not make a difference...

i'll take the asp out if this can help you, but doens't interfear with the rest...





<script language="javascript">	 
row_no=1; 
function addRow(tbl,row){ 
        if(row_no<=500){ 
		var bolly= "box" + row_no; 
        var oms1= "desa" + row_no;
		var oms2= "desb" + row_no;
		var oms2z= "desbz" + row_no;
		var oms3= "desc" + row_no;
		var oms3z= "descz" + row_no;
		var getal1= "getala" + row_no;
		var getal2= "getalb" + row_no;
		var getal3= "getalc" + row_no;
		var getal4= "getald" + row_no;
		var getal5= "getale" + row_no;
		var btwset= "btwa" + row_no;
		var subtota= "subtot" + row_no;
		var typelijn= "typelijn" + row_no;
		var textbolly='<input name=' + bolly + ' type="checkbox" type="type1z"  value="" onClick="if(document.banza.' + bolly + '.checked) {document.banza.' + bolly + '.value=document.banza.' + subtota +'.value;}else{document.banza.' + bolly + '.value=0;}">';
		if(document.banza.whatkind.value == "dsvint"){
		var textoms1='<input type="text" class="type1z" name=' + oms1 + ' size="10" maxlength="10" value="<%=strDate%>">';}
		if(document.banza.whatkind.value == "dsvint"){
		var textoms2='<input type="text" name=' + oms2 + ' class="type1z" id=' + oms2 + ' size="40" maxlength="100" value="00 UREN 00 MIN EN 000 KM">';}
		if(document.banza.whatkind.value == "dsvint"){
		var textoms3='<input type="text" name=' + oms3 + ' class="type1z" id=' + oms3 + ' size="40" maxlength="100" value="TOER 00-1111">';}
		var textsub='<input type="text" name=' + subtota + ' class="type2z" id=' + subtota + ' size="10" maxlength="30" value="" onchange="document.banza.' + bolly + '.checked=false; document.banza.' + bolly + '.value =0;">';
		var textbtw='<select name=' + btwset + ' class="type1z" id=' + btwset +'><OPTION VALUE="21">21</OPTION><OPTION VALUE="6">6</OPTION></select>';	
		var remove= '<a href="javascript:void(null)" onclick="removeRow(\''+ tbl +'\',\'' + row_no + '\')"/><img src="Images/del.gif" border="0" align="absmiddle"/></a>';    
        			
        var tbl = document.getElementById(tbl);//to identify the table in which the row will get insert 
        var rowIndex = document.getElementById(row).value;//to identify the row after which the row will be inserted 
        try { 
            var newRow = tbl.insertRow(row_no);//creation of new row 
            var newCell = newRow.insertCell(0);//first  cell in the row  
            var newCell = newRow.insertCell(1);//second cell in the row 
			if(document.banza.whatkind.value == "dsvint")
			{
            newCell.innerHTML = "<table width='600' border='0' cellspacing='0' cellpadding='0'><tr><td valign='top'>"+ remove + "</td><td valign='top'><p class='contacttable'>"+ textbolly + "</p></td><td valign='top'><p class='contacttable'>" + textoms1 + "</p></td><td valign='top'><p class='contacttable'>" + textoms2 + "<br>" + textoms3 + "</p></td><td valign='top'><p class='contacttable'>" + textsub + "¬</p></td><td valign='top'><p class='contacttable'>" + textbtw + "</p></td></tr><tr><td colspan='6'><hr style='border=thin;color=#666666'/></td></tr></table>";
			}			banza.factor.value=row_no;
            row_no++;
        } catch (ex) { 
            alert(ex); //if exception occurs 
        } 
            
    } 
    if(row_no>500 { 
        document.getElementById("add").style.display="none"; 
    }                        
} 
function removeRow(tbl,num) 
{ 
    var table = document.getElementById(tbl);//adentification of table 
    try { 
        row_no--; 
        table.deleteRow(num);//deletion of the clicked row 
		banza.factor.value=row_no-1;
    } catch (ex) { 
        alert(ex); 
    } 
    
    if(row_no<=500)//if row is less than 3 then the button will again appear to add row 
    { 
            document.getElementById("add").style.display="block"; 
    }    
} 
</script>
<script language="JavaScript" type="text/javascript">
function CalculateTotal(frm) {
    var order_total = 0
 
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
 
        // Get the current field
        form_field = frm.elements[i]
 
        // Get the field's name
        form_name = form_field.name
 
        // Is it a "product" field?
		if (form_name.substring(0,3) == "box") {
		
		
            // Get the quantity
            item_quantity =form_field.value
			item_quantity=item_quantity.replace(/,/g,".")
        item_price = 1
		
                order_total += item_quantity * item_price
        }
		
		
	
		
    }
 
    // Display the total rounded to two decimal places
	frm.totaalz21.value= Math.round(order_total*100)/100;
}
 
function calctotal() { 
  var soma=0;
  var theForm = document.forms.banza;
  for(var i=0;txt=theForm['btwa'+i];i++){
    if(txt.value==21) { 
      var subTot = theForm['subtot'+i].value * 1;
      if(!isNaN(subTot)) soma = soma + subTot;
    } 
  }
  theForm.totalm.value = soma; 
} 
</script>
 
<form name="banza" id="banza">
...
<table width="600" border="0" cellspacing="0" id="mytable">
<tr id="myrow"></tr></table>
<div id="add"><p class="contacttablew">&raquo;&nbsp;&nbsp;+<a class="table" href="javascript:void(null)" onClick="document.banza.whatkind.value='dsvint'; addRow('mytable','myrow')">DSVint</a>&nbsp;&nbsp</p></div>
<table width="600" border="0" cellspacing="0">
<tr><td></td><td><p class="contacttable"><br><input type="text" name="totaalz21"><input onClick="CalculateTotal(this.form); if(document.banza.totaalz21.value.indexOf('.') >= 0) {
document.banza.totaalz21.value = document.banza.totaalz21.value.replace(/\./g,',');}" name="OK" value="bereken" type="button"><br>
<input type="text" name="totalm"><input onClick="calctotal(this.form);" name="OK" value="bereken" type="button">

Open in new window

0
 

Author Comment

by:frippel
ID: 20399606
or more complete...
<script language="javascript">	 
row_no=1; 
function addRow(tbl,row){ 
        if(row_no<=500){ 
		var bolly= "box" + row_no; 
        var oms1= "desa" + row_no;
		var oms2= "desb" + row_no;
		var oms2z= "desbz" + row_no;
		var oms3= "desc" + row_no;
		var oms3z= "descz" + row_no;
		var getal1= "getala" + row_no;
		var getal2= "getalb" + row_no;
		var getal3= "getalc" + row_no;
		var getal4= "getald" + row_no;
		var getal5= "getale" + row_no;
		var btwset= "btwa" + row_no;
		var subtota= "subtot" + row_no;
		var typelijn= "typelijn" + row_no;
		var textbolly='<input name=' + bolly + ' type="checkbox" type="type1z"  value="" onClick="if(document.banza.' + bolly + '.checked) {document.banza.' + bolly + '.value=document.banza.' + subtota +'.value;}else{document.banza.' + bolly + '.value=0;}">';
		if(document.banza.whatkind.value == "dsvint"){
		var textoms1='<input type="text" class="type1z" name=' + oms1 + ' size="10" maxlength="10" value="<%=strDate%>">';}
		if(document.banza.whatkind.value == "dsvint"){
		var textoms2='<input type="text" name=' + oms2 + ' class="type1z" id=' + oms2 + ' size="40" maxlength="100" value="00 UREN 00 MIN EN 000 KM">';}
		if(document.banza.whatkind.value == "dsvint"){
		var textoms3='<input type="text" name=' + oms3 + ' class="type1z" id=' + oms3 + ' size="40" maxlength="100" value="TOER 00-1111">';}
		var textsub='<input type="text" name=' + subtota + ' class="type2z" id=' + subtota + ' size="10" maxlength="30" value="" onchange="document.banza.' + bolly + '.checked=false; document.banza.' + bolly + '.value =0;">';
		var textbtw='<select name=' + btwset + ' class="type1z" id=' + btwset +'><OPTION VALUE="21">21</OPTION><OPTION VALUE="6">6</OPTION></select>';	
		var remove= '<a href="javascript:void(null)" onclick="removeRow(\''+ tbl +'\',\'' + row_no + '\')"/><img src="Images/del.gif" border="0" align="absmiddle"/></a>';    
        			
        var tbl = document.getElementById(tbl);//to identify the table in which the row will get insert 
        var rowIndex = document.getElementById(row).value;//to identify the row after which the row will be inserted 
        try { 
            var newRow = tbl.insertRow(row_no);//creation of new row 
            var newCell = newRow.insertCell(0);//first  cell in the row  
            var newCell = newRow.insertCell(1);//second cell in the row 
			if(document.banza.whatkind.value == "dsvint")
			{
            newCell.innerHTML = "<table width='600' border='0' cellspacing='0' cellpadding='0'><tr><td valign='top'>"+ remove + "</td><td valign='top'><p class='contacttable'>"+ textbolly + "</p></td><td valign='top'><p class='contacttable'>" + textoms1 + "</p></td><td valign='top'><p class='contacttable'>" + textoms2 + "<br>" + textoms3 + "</p></td><td valign='top'><p class='contacttable'>" + textsub + "¬</p></td><td valign='top'><p class='contacttable'>" + textbtw + "</p></td></tr><tr><td colspan='6'><hr style='border=thin;color=#666666'/></td></tr></table>";
			}			banza.factor.value=row_no;
            row_no++;
        } catch (ex) { 
            alert(ex); //if exception occurs 
        } 
            
    } 
    if(row_no>500 { 
        document.getElementById("add").style.display="none"; 
    }                        
} 
function removeRow(tbl,num) 
{ 
    var table = document.getElementById(tbl);//adentification of table 
    try { 
        row_no--; 
        table.deleteRow(num);//deletion of the clicked row 
		banza.factor.value=row_no-1;
    } catch (ex) { 
        alert(ex); 
    } 
    
    if(row_no<=500)//if row is less than 3 then the button will again appear to add row 
    { 
            document.getElementById("add").style.display="block"; 
    }    
} 
</script>
<script language="JavaScript" type="text/javascript">
function CalculateTotal(frm) {
    var order_total = 0
 
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {
 
        // Get the current field
        form_field = frm.elements[i]
 
        // Get the field's name
        form_name = form_field.name
 
        // Is it a "product" field?
		if (form_name.substring(0,3) == "box") {
		
		
            // Get the quantity
            item_quantity =form_field.value
			item_quantity=item_quantity.replace(/,/g,".")
        item_price = 1
		
                order_total += item_quantity * item_price
        }
		
		
	
		
    }
 
    // Display the total rounded to two decimal places
	frm.totaalz21.value= Math.round(order_total*100)/100;
}
 
function calctotal() { 
  var soma=0;
  var theForm = document.forms.banza;
  for(var i=0;txt=theForm['btwa'+i];i++){
    if(txt.value==21) { 
      var subTot = theForm['subtot'+i].value * 1;
      if(!isNaN(subTot)) soma = soma + subTot;
    } 
  }
  theForm.totalm.value = soma; 
} 
</script>
 
<form name="banza" id="banza">
<table width="600" border="0" cellspacing="0">
 <tr><td colspan="2"><H3>Factuur Toevoegen</H3> </td></tr>
  <tr><td><p class="contactright">Klant:</p></td><td><p class="contact">
  <input name="mwaarde" type="hidden" size="10" maxlength="10" value="<%=m%>">
	<input name="cwaarde" type="hidden" size="10" maxlength="10" value="<%=c%>">
	<input name="datezet" type="hidden" size="10" maxlength="10" value="<%=strDate%>">
	<input name="weekzet" type="hidden" size="10" maxlength="10" value="week x">
	<input name="leegzet" type="hidden" size="10" maxlength="10" value="">
	<input name="factor" type="hidden" size="50" maxlength="100" value="1">
	<input name="whata" type="hidden" size="20" maxlength="20" value="auto">
	<input name="whatb" type="hidden" size="20" maxlength="20" value="prijsauto">
	<input name="whatc" type="hidden" size="20" maxlength="20" value="prijsauto">
	<input name="whatd" type="hidden" size="20" maxlength="20" value="prijsauto">
	<input name="whate" type="hidden" size="20" maxlength="20" value="prijsauto">
	<input name="whatf" type="hidden" size="20" maxlength="20" value="prijsauto">
	<input name="whatkind" type="hidden" size="20" maxlength="20" value=""></td></tr>/table>
 
<table width="600" border="0" cellspacing="0" id="mytable">
<tr id="myrow"></tr></table>
<div id="add"><p class="contacttablew">&raquo;&nbsp;&nbsp;+<a class="table" href="javascript:void(null)" onClick="document.banza.whatkind.value='dsvint'; addRow('mytable','myrow')">DSVint</a>&nbsp;&nbsp</p></div>
<table width="600" border="0" cellspacing="0">
<tr><td></td><td><p class="contacttable"><br><input type="text" name="totaalz21"><input onClick="CalculateTotal(this.form); if(document.banza.totaalz21.value.indexOf('.') >= 0) {
document.banza.totaalz21.value = document.banza.totaalz21.value.replace(/\./g,',');}" name="OK" value="bereken" type="button"><br>
<input type="text" name="totalm"><input onClick="calctotal(this.form);" name="OK" value="bereken" type="button">

Open in new window

0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 400 total points
ID: 20401380
Change the starting index from zero to one and you are done.
0
 

Author Closing Comment

by:frippel
ID: 31412284
so final script

function calctotal() {
  var soma=0;
  var theForm = document.forms.banza;
  for(var i=1;txt=theForm['btwa'+i];i++){
    if(txt.value==21) {
      var subTot = theForm['subtot'+i].value * 1;
      if(!isNaN(subTot)) soma = soma + subTot;
    }
  }
  theForm.totalm21.value = soma;
}
0
 

Author Comment

by:frippel
ID: 20401442
Excellent Zvonko. Thank you very much!!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20406344
You are welcome :)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question