Solved

Problems with sum in javascript

Posted on 2007-12-02
21
335 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
  • 11
  • 10
21 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:frippel
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

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

Author Comment

by:frippel
Comment Utility
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
Comment Utility
Is you page accessible online?
0
 

Author Comment

by:frippel
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
Change the starting index from zero to one and you are done.
0
 

Author Closing Comment

by:frippel
Comment Utility
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
Comment Utility
Excellent Zvonko. Thank you very much!!
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
You are welcome :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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. …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now