473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3277
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

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

Similar topics

7
by: Aaron Prohaska | last post by:
I have just run into a problem where I have a page that posts back to itself to execute code, except when the page does the post back it somehow executes code that is in our home page for the site....
4
by: Federico Bari | last post by:
Good morning all from italy, i have probably a compatibility problem with a html/javascript page. The aim of the code of the file test.htm you find here following (copy the 3 files in the...
1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
8
by: rdlebreton | last post by:
Hi, Folks! I've been trying to develop my own version of these draggable layers and I have been limiting myself to IE6...for now. I have looked at some other examples to get ideas of creating...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
1
by: mostafahamdyfcis | last post by:
Hello all I have used the sample code which exist in the following URL: http://www.eggheadcafe.com/articles/javascript_modal_dialog.asp which create modal dialog box, but some problem...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
11
by: jessy | last post by:
Hi, I have a problem with my DateTimePicker javascript code which i downloaded , the problem is when i pick the date and the date appears in my Text Field and i click Submit the date which i picked...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.