By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Problem with function Javascript Sum

100+
P: 263
Hi there.

I have this Function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Funzione Calcola
  3.  
  4. function calcola(frmObj, id)
  5.  
  6. {
  7.  
  8.  
  9. //Somma Totale Imponibile
  10.  
  11. var somma_totale_imponibile = 0;
  12. var nelementi = ["Totale_imponibile_" + id].length ;
  13.  
  14. for ( var n = 0; n <= nelementi; n++ )
  15.  
  16.     {
  17.         n = parseFloat(frmObj["Totale_imponibile_" + id].value);
  18.         somma_totale_imponibile += ((isNaN(parseFloat(n)))?0:parseFloat(n)); 
  19.         alert(n)
  20.     }  
  21.  
  22.     alert("Totale_imponibile_" + id)
  23.     alert(somma_totale_imponibile)
  24.  
  25.     frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2); 
  26.  
  27.  
  28. //chiusura funzione calcola
  29.  
  30.  

I require this datum:

Totale_imponibile_1 = 3
Totale_imponibile_2 = 50
Totale_imponibile_3 = 2

somma_totale_imponibile = 55

It doesn't work.

Can anyone help?

Thanks, regards
Viki
Oct 27 '07 #1
Share this Question
Share on Google+
19 Replies


gits
Expert Mod 5K+
P: 5,282
hi ...

welcome to TSDN ...

i think that doesn't work the way you intended. please show your html relating to this problem too ... because i assume nelementi should be a number of textboxes contained by your form?

kind regards
Oct 27 '07 #2

Ferris
100+
P: 101
hi

well,I think there's a logical error in your code,but I can see what you want to do.so I write a code for you.I hope it's you want.

[HTML]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<form id="form1">
<input type="text" id="Totale_imponibile_1" value="50" /><br />
<input type="text" id="Totale_imponibile_2" value="2" /><br />
<input type="text" id="Totale_imponibile_3" value="3" /><br />
<input type="text" id="somma_totale_imponibile" value="" /><br />
<input type="button" value="Sum Up" onClick="calcola(3);" />
</form>
</body>

<script language="javascript">
function calcola(length)
{
//Somma Totale Imponibile
var somma_totale_imponibile = 0;
for ( var id = 1; id <= length; id++ )
{
var temp = document.getElementById("Totale_imponibile_" + id).value;
if (isNaN(temp) == false)
somma_totale_imponibile += parseFloat(temp);
}
//alert(somma_totale_imponibile);
document.getElementById("somma_totale_imponibile") .value = somma_totale_imponibile.toFixed(2);
}
</script>
</html>



[/HTML]


hope it helps.
Oct 28 '07 #3

100+
P: 263
Thanks for you help, but sorry iam unable to work code.

Address page htm:
http://users2.titanichost.com/garza/
Oct 29 '07 #4

100+
P: 263
Can anyone help?

Regards
Viki
Oct 30 '07 #5

acoder
Expert Mod 15k+
P: 16,027
You're still starting at 0 in your loop. The first number should be 1.
Oct 30 '07 #6

100+
P: 263
Thanks, but not work:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. <!--
  3.  
  4.  
  5. //Funzione Calcola
  6.  
  7. function calcola(frmObj, id)
  8. {
  9.  
  10. //Totale Imponibile
  11.   frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["quantita_" + id].value) *
  12.                                             parseFloat(frmObj["imponibile_meno_sconto_" + id].value);                                            
  13.   frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["Totale_imponibile_" + id].value).toFixed(2);
  14.  
  15.  
  16. //Totale IVA
  17.   frmObj["Tot_iva_" + id].value = ((parseFloat(frmObj["Totale_imponibile_" + id].value)/100) *
  18.                                     parseFloat(frmObj["Iva_" + id].value));  
  19.   frmObj["Tot_iva_" + id].value = parseFloat(frmObj["Tot_iva_" + id].value).toFixed(2);
  20.  
  21.  
  22. //Somma Totale Imponibile
  23.  
  24. var somma_totale_imponibile = 0;
  25. var nelementi = ["Totale_imponibile_" + id].length ;
  26.  
  27. for ( var n = 1; n <= nelementi; n++ )
  28.  
  29.     {
  30.         n = parseFloat(frmObj["Totale_imponibile_" + id].value);
  31.         somma_totale_imponibile += ((isNaN(parseFloat(n)))?0:parseFloat(n)); 
  32.  
  33.     }  
  34.  
  35.     alert("Totale_imponibile_" + id)
  36.     alert(somma_totale_imponibile)
  37.  
  38.     frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2);        
  39.  
  40.  
  41.  
  42. //chiusura funzione calcola
  43.  
  44. // -->
  45. </script>
Oct 30 '07 #7

acoder
Expert Mod 15k+
P: 16,027
nelementi is not set correctly. Set that using the number of rows in the table or just use 9 (if its going to remain fixed).

In the for loop, use another variable name besides "n". You've already used it for indexing. Replace "id" with "n", so that you're not just accessing the same text box each time.
Oct 30 '07 #8

100+
P: 263
Nothing... no work:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. <!--
  3.  
  4.  
  5. //Funzione Calcola
  6.  
  7. function calcola(frmObj, id)
  8. {
  9.  
  10. //Totale Imponibile
  11.   frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["quantita_" + id].value) *
  12.                                             parseFloat(frmObj["imponibile_meno_sconto_" + id].value);                                            
  13.   frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["Totale_imponibile_" + id].value).toFixed(2);
  14.  
  15.  
  16. //Totale IVA
  17.   frmObj["Tot_iva_" + id].value = ((parseFloat(frmObj["Totale_imponibile_" + id].value)/100) *
  18.                                     parseFloat(frmObj["Iva_" + id].value));  
  19.   frmObj["Tot_iva_" + id].value = parseFloat(frmObj["Tot_iva_" + id].value).toFixed(2);
  20.  
  21.  
  22. //Somma Totale Imponibile
  23.  
  24. var somma_totale_imponibile = 0;
  25. var nelementi = ["Totale_imponibile_" + id].length ;
  26.  
  27. for ( var n = 1; n <= nelementi; n++ )
  28.  
  29.     {
  30.         n = parseFloat(frmObj["Totale_imponibile_" + n].value);
  31.         somma_totale_imponibile += ((isNaN(parseFloat(n)))?0:parseFloat(n)); 
  32.  
  33.     }  
  34.  
  35.     alert("Totale_imponibile_" + id)
  36.     alert(somma_totale_imponibile)
  37.  
  38.     frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2);        
  39.  
  40.  
  41.  
  42. //chiusura funzione calcola
  43.  
  44. // -->
  45. </script>
  46.  
  47.  
Oct 30 '07 #9

acoder
Expert Mod 15k+
P: 16,027
You've not made the changes.

On line 25, you're setting nelementi.

On lines 30-31, you will also need to make changes as I described earlier.
Oct 30 '07 #10

100+
P: 263
Yuo write:

In the for loop, use another variable name besides "n". You've already used it for indexing. Replace "id" with "n", so that you're not just accessing the same text box each time.


Expand|Select|Wrap|Line Numbers
  1. for ( var n = 1; n <= nelementi; n++ )
  2.  
  3.     {
  4.         x = parseFloat(frmObj["Totale_imponibile_" + n].value);
  5.         somma_totale_imponibile += ((isNaN(parseFloat(x)))?0:parseFloat(x)); 
  6.  
  7.     }  
  8.  
  9.  
What I do not understand ?
Thanks
Oct 30 '07 #11

acoder
Expert Mod 15k+
P: 16,027
Try using 9 instead of nelementi, so that you can at least test that it's working.

In your latest code, on line 5, you don't need to parseFloat it again because that has already been done on the previous line.
Oct 30 '07 #12

100+
P: 263
Try using 9 instead of nelementi, so that you can at least test that it's working.
Not working.

In your latest code, on line 5, you don't need to parseFloat it again because that has already been done on the previous line.
Expand|Select|Wrap|Line Numbers
  1. for ( var n = 1; n <= nelementi; n++ )
  2.  
  3.     {
  4.         n = parseFloat(frmObj["Totale_imponibile_" + id].value);
  5.         somma_totale_imponibile += n; 
  6.  
  7.     }  
  8.  
  9.     alert("Totale_imponibile_" + id)
  10.     alert(somma_totale_imponibile)
  11.  
  12.     frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2); 
  13.  
Not working.

Sorry.
Oct 30 '07 #13

acoder
Expert Mod 15k+
P: 16,027
OK, try this:
Expand|Select|Wrap|Line Numbers
  1. for ( var n = 1; n <= 9; n++ )
  2.     {
  3.         x = parseFloat(frmObj["Totale_imponibile_" + n].value);
  4.         somma_totale_imponibile += ((isNaN(x))?0:x);
  5.     }
Oct 30 '07 #14

100+
P: 263
Ok this work thanks; but nelementi it is not always fixed 9.
It is a variable in the code...
Oct 30 '07 #15

acoder
Expert Mod 15k+
P: 16,027
Ok this work thanks; but nelementi it is not always fixed 9.
It is a variable in the code...
Yes, I know that, but at least one problem is fixed.

To make nelementi variable, use the table's rows[] array:
Expand|Select|Wrap|Line Numbers
  1. var nelementi = document.getElementById("tableid").rows.length-2;
Oct 31 '07 #16

100+
P: 263
Sorry, but not working...

Expand|Select|Wrap|Line Numbers
  1. var somma_totale_imponibile = 0;
  2. //var nelementi = ["Totale_imponibile_" + id].length ;
  3. var nelementi = document.getElementById("Totale_imponibile_").rows.length-2;
  4.  
  5. for ( var n = 1; n <= nelementi; n++ )
  6.  
  7.     {
  8.         x = parseFloat(frmObj["Totale_imponibile_" + n].value);
  9.         somma_totale_imponibile += ((isNaN(x))?0:x);
  10.     } 
  11.  
  12.     alert("Totale_imponibile_" + id)
  13.     alert(somma_totale_imponibile)
  14.  
  15.     frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2);  
  16.  
Oct 31 '07 #17

acoder
Expert Mod 15k+
P: 16,027
You're using the wrong id. The table id according to the source code is "table3".
Oct 31 '07 #18

100+
P: 263
Yes, this working !!!!!

You magician !!!

many thanks
Viki1967
Oct 31 '07 #19

acoder
Expert Mod 15k+
P: 16,027
No problem, you're welcome. I've been called many things, but never a magician!

Post again if you have any more questions.
Oct 31 '07 #20

Post your reply

Sign in to post your reply or Sign up for a free account.