469,309 Members | 2,757 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

Problem with function Javascript Sum

263 100+
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
19 2487
gits
5,390 Expert Mod 4TB
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
101 100+
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
viki1967
263 100+
Thanks for you help, but sorry iam unable to work code.

Address page htm:
http://users2.titanichost.com/garza/
Oct 29 '07 #4
viki1967
263 100+
Can anyone help?

Regards
Viki
Oct 30 '07 #5
acoder
16,027 Expert Mod 8TB
You're still starting at 0 in your loop. The first number should be 1.
Oct 30 '07 #6
viki1967
263 100+
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
16,027 Expert Mod 8TB
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
viki1967
263 100+
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
16,027 Expert Mod 8TB
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
viki1967
263 100+
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
16,027 Expert Mod 8TB
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
viki1967
263 100+
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
16,027 Expert Mod 8TB
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
viki1967
263 100+
Ok this work thanks; but nelementi it is not always fixed 9.
It is a variable in the code...
Oct 30 '07 #15
acoder
16,027 Expert Mod 8TB
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
viki1967
263 100+
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
16,027 Expert Mod 8TB
You're using the wrong id. The table id according to the source code is "table3".
Oct 31 '07 #18
viki1967
263 100+
Yes, this working !!!!!

You magician !!!

many thanks
Viki1967
Oct 31 '07 #19
acoder
16,027 Expert Mod 8TB
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.

Similar topics

7 posts views Thread by Aaron Prohaska | last post: by
4 posts views Thread by Federico Bari | last post: by
4 posts views Thread by JesusFreak | last post: by
8 posts views Thread by rdlebreton | last post: by
2 posts views Thread by sorobor | last post: by
11 posts views Thread by jessy | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.