Problem with function Javascript Sum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    Problem with function Javascript Sum

    Hi there.

    I have this Function:

    Code:
    //Funzione Calcola
    
    function calcola(frmObj, id)
    
    {
    
    
    //Somma Totale Imponibile
    
    var somma_totale_imponibile = 0;
    var nelementi = ["Totale_imponibile_" + id].length ;
    
    for ( var n = 0; n <= nelementi; n++ )
    
        {
            n = parseFloat(frmObj["Totale_imponibile_" + id].value);
            somma_totale_imponibile += ((isNaN(parseFloat(n)))?0:parseFloat(n)); 
            alert(n)
        }  
        
        alert("Totale_imponibile_" + id)
        alert(somma_totale_imponibile)
    
        frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2); 
    
    
    //chiusura funzione calcola
    }

    I require this datum:

    Totale_imponibi le_1 = 3
    Totale_imponibi le_2 = 50
    Totale_imponibi le_3 = 2

    somma_totale_im ponibile = 55

    It doesn't work.

    Can anyone help?

    Thanks, regards
    Viki
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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

    Comment

    • Ferris
      New Member
      • Oct 2007
      • 101

      #3
      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>Untitl ed Document</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      </head>

      <body>
      <form id="form1">
      <input type="text" id="Totale_impo nibile_1" value="50" /><br />
      <input type="text" id="Totale_impo nibile_2" value="2" /><br />
      <input type="text" id="Totale_impo nibile_3" value="3" /><br />
      <input type="text" id="somma_total e_imponibile" value="" /><br />
      <input type="button" value="Sum Up" onClick="calcol a(3);" />
      </form>
      </body>

      <script language="javas cript">
      function calcola(length)
      {
      //Somma Totale Imponibile
      var somma_totale_im ponibile = 0;
      for ( var id = 1; id <= length; id++ )
      {
      var temp = document.getEle mentById("Total e_imponibile_" + id).value;
      if (isNaN(temp) == false)
      somma_totale_im ponibile += parseFloat(temp );
      }
      //alert(somma_tot ale_imponibile) ;
      document.getEle mentById("somma _totale_imponib ile").value = somma_totale_im ponibile.toFixe d(2);
      }
      </script>
      </html>



      [/HTML]


      hope it helps.

      Comment

      • viki1967
        Contributor
        • Oct 2007
        • 263

        #4
        Thanks for you help, but sorry iam unable to work code.

        Address page htm:

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Can anyone help?

          Regards
          Viki

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You're still starting at 0 in your loop. The first number should be 1.

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Thanks, but not work:

              Code:
              <script language="javascript">
              <!--
              
              
              //Funzione Calcola
              
              function calcola(frmObj, id)
              {
              
              //Totale Imponibile
                frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["quantita_" + id].value) *
                                                          parseFloat(frmObj["imponibile_meno_sconto_" + id].value);                                            
                frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["Totale_imponibile_" + id].value).toFixed(2);
              
              
              //Totale IVA
                frmObj["Tot_iva_" + id].value = ((parseFloat(frmObj["Totale_imponibile_" + id].value)/100) *
                                                  parseFloat(frmObj["Iva_" + id].value));  
                frmObj["Tot_iva_" + id].value = parseFloat(frmObj["Tot_iva_" + id].value).toFixed(2);
                
              
              //Somma Totale Imponibile
              
              var somma_totale_imponibile = 0;
              var nelementi = ["Totale_imponibile_" + id].length ;
              
              for ( var n = 1; n <= nelementi; n++ )
              
                  {
                      n = parseFloat(frmObj["Totale_imponibile_" + id].value);
                      somma_totale_imponibile += ((isNaN(parseFloat(n)))?0:parseFloat(n)); 
              
                  }  
                  
                  alert("Totale_imponibile_" + id)
                  alert(somma_totale_imponibile)
              
                  frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2);        
                      
                      
              
              //chiusura funzione calcola
              } 
              
              // -->
              </script>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                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.

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #9
                  Nothing... no work:

                  Code:
                  <script language="javascript">
                  <!--
                  
                  
                  //Funzione Calcola
                  
                  function calcola(frmObj, id)
                  {
                  
                  //Totale Imponibile
                    frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["quantita_" + id].value) *
                                                              parseFloat(frmObj["imponibile_meno_sconto_" + id].value);                                            
                    frmObj["Totale_imponibile_" + id].value = parseFloat(frmObj["Totale_imponibile_" + id].value).toFixed(2);
                  
                  
                  //Totale IVA
                    frmObj["Tot_iva_" + id].value = ((parseFloat(frmObj["Totale_imponibile_" + id].value)/100) *
                                                      parseFloat(frmObj["Iva_" + id].value));  
                    frmObj["Tot_iva_" + id].value = parseFloat(frmObj["Tot_iva_" + id].value).toFixed(2);
                    
                  
                  //Somma Totale Imponibile
                  
                  var somma_totale_imponibile = 0;
                  var nelementi = ["Totale_imponibile_" + id].length ;
                  
                  for ( var n = 1; n <= nelementi; n++ )
                  
                      {
                          n = parseFloat(frmObj["Totale_imponibile_" + n].value);
                          somma_totale_imponibile += ((isNaN(parseFloat(n)))?0:parseFloat(n)); 
                  
                      }  
                      
                      alert("Totale_imponibile_" + id)
                      alert(somma_totale_imponibile)
                  
                      frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2);        
                          
                          
                  
                  //chiusura funzione calcola
                  } 
                  
                  // -->
                  </script>

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    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.

                    Comment

                    • viki1967
                      Contributor
                      • Oct 2007
                      • 263

                      #11
                      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.


                      Code:
                      for ( var n = 1; n <= nelementi; n++ )
                      
                          {
                              x = parseFloat(frmObj["Totale_imponibile_" + n].value);
                              somma_totale_imponibile += ((isNaN(parseFloat(x)))?0:parseFloat(x)); 
                      
                          }
                      What I do not understand ?
                      Thanks

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        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.

                        Comment

                        • viki1967
                          Contributor
                          • Oct 2007
                          • 263

                          #13
                          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.
                          Code:
                          for ( var n = 1; n <= nelementi; n++ )
                          
                              {
                                  n = parseFloat(frmObj["Totale_imponibile_" + id].value);
                                  somma_totale_imponibile += n; 
                          
                              }  
                              
                              alert("Totale_imponibile_" + id)
                              alert(somma_totale_imponibile)
                          
                              frmObj.somma_totale_imponibile.value = somma_totale_imponibile.toFixed(2);
                          Not working.

                          Sorry.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            OK, try this:[code=javascript]
                            for ( var n = 1; n <= 9; n++ )
                            {
                            x = parseFloat(frmO bj["Totale_imponib ile_" + n].value);
                            somma_totale_im ponibile += ((isNaN(x))?0:x );
                            }[/code]

                            Comment

                            • viki1967
                              Contributor
                              • Oct 2007
                              • 263

                              #15
                              Ok this work thanks; but nelementi it is not always fixed 9.
                              It is a variable in the code...

                              Comment

                              Working...