Total Sum Question.. OnBlur? OnChange? Why does it not display?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mozzi
    New Member
    • May 2010
    • 4

    #1

    Total Sum Question.. OnBlur? OnChange? Why does it not display?

    I have a form. Diffrent fields that add up to a sub total. I need to sub totals to autocomplete the TOTAL sum??? I have tried a few things.. but it did not work. Im quite new to this.. I have used some code examples from the site.. (thanks)...

    I just need to TOTAL SUM to display when i add some numericals to the top fields?? where am I going wrong???

    Code:
    <html>
    <head>
    
    <script type="text/JavaScript">
    function calculate(col, row){
    var numericExpression = /^[0-9]+$/; //verify numeric - needs to allow for decimals though...hmmm...
        if(document.getElementById(col+"-"+row).value.match(numericExpression)){
    var input=0;  //initalize variable to get entered value
    var rowTot=0; //initalize variable to capture row totals
    var colTot=0; //initalize variable to capture column totals
    //setup up array for paycodes/column headers 
    codes=new Array('1000','1001','1002','1005','1007','1003','1020','1035','1038','1045','1066','1067','Other'); 
    //loop through all of the text fields for a given column and total them
            for (i=0;i<2;i++){
            input=(document.getElementById(col+'-'+i).value)*1
            colTot=colTot+input;
            document.getElementById(col).value = colTot;
            }
    //loop through all of the text fields for a given row and total them
            for (j=0;j<13;j++){
            input=(document.getElementById(((codes[j])+'-'+row)).value)*1
            rowTot=rowTot+input;
            document.getElementById('row'+row).value = rowTot;
            }
        }
        //if the entry isn't numeric then return no value
        else {document.getElementById(col+"-"+row).value="";}
    }
    
    function updatesum() {
    document.form.sum.value = (document.form.sum1.value -0) + (document.form.sum2.value -0);
    }
    
    </script>
    
    </head>
    
    <body>
    
    <p>Enter numbers here:<br>
    <form name="form"  >
      <p>
        <input name="1000-0" id="1000-0" type="text"  onblur="calculate('1000','0')" >
        <input name="1001-0" id="1001-0" type="text"  onblur="calculate('1001','0')" >
        <br>
        <input name="1000-1" id="1000-1" type="text"  onblur="calculate('1000','1')" >
        <input name="1001-1" id="1001-1" type="text"  onblur="calculate('1001','1')" >
        <br>
        <br />
        sub total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub total<br />
        <input name="sum1" id="1000" type="text" onblur="updatesum()" >
        <input name="sum2" id="1001" type="text" onblur="updatesum()" >
        <br />
        <br />
        Total Sum:
        <input name="sum" readonly  />
    </form>
    </body>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Within calculate, call updatesum() as well. Onchange fires when the field has focus, has been changed and then loses focus, which wouldn't work here.

    Comment

    • Mozzi
      New Member
      • May 2010
      • 4

      #3
      ooohhh ok... well i have to find a diffrent solution then.. thanks for the time.. ill lool for something else then :) :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Look into mutation events. A bit of advanced stuff that is not well supported at present. It may be what you're after.

        Comment

        • Mozzi
          New Member
          • May 2010
          • 4

          #5
          thanks will do :)

          Comment

          Working...