calculate on the fly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • techker
    New Member
    • Aug 2014
    • 3

    calculate on the fly

    Hey guys im doing a POS for my body's store and im really bitting my nails on the form total calculations.

    this is a test script
    but it only calculates the grand total not the form part

    Code:
     <script type="text/javascript">
    
    ///////////this is for filed total
    		function calc(A,B,SUM) { 
      var one = Number(A); 
      if (isNaN(one)) { alert('Invalid entry: '+A); one=0; } 
      var two = Number(document.getElementById(B).value);  
      if (isNaN(two)) { alert('Invalid entry: '+B); two=0; } 
      document.getElementById(SUM).value = one + two; 
    }
    
    
    /////////////this is for grand total 
    function calcTotal(txtBox, totBox)
    {
        var totVal;
        try
        { 
        totVal = document.getElementById(totBox).value;
        if(totVal!= null && totVal!='')
         {
           document.getElementById(totBox).value= eval(parseInt(document.getElementById(totBox).value) + parseInt(txtBox.value));   
         }
         else
         {
            document.getElementById(totBox).value= txtBox.value;           
         }
        }
        catch(e)
        {}
    }
    they both work individual but when i add one it stops the filed calculator

    Code:
    <table> 
    <tr> 
    <td width="337"><input name="sum1" id="op1" value="" onChange="calc(this.value,'op2','result')" />  <input name="sum2" value="" id="op2" onChange="calc(this.value,'op1','result')" /></td> 
    <td width="176"><input type="text" id="result" onBlur="calcTotal(this, 'tot')" /></td> 
    </tr> 
    <tr>     
    <td><input name="op" id="op3" value="" onchange="calc(this.value,'op4','result2')" /><input name="op" value="" id="op4" onchange="calc(this.value,'op3','result2')" /></td> 
    <td><input type="text" id="result2" onBlur="calcTotal(this, 'tot')" /></td> 
    </tr> 
    <tr>     
    <td> <input name="op" id="op5" value="" onchange="calc(this.value,'op5','result3')" /><input name="op" value="" id="op6" onchange="calc(this.value,'op6','result3')" /></td> 
    <td><input type="text" id="result3" onBlur="calcTotal(this, 'tot')" /></td>        
    </tr> 
    <tr>     
    <td>Total</td> 
    <td><input type="text" id="tot" /></td> 
    </tr>    
    </table>
  • techker
    New Member
    • Aug 2014
    • 3

    #2
    ok so i got this to work

    but i need to calculate taxes.do i need to creat a var ta1 and tax 2?

    Code:
    <script type="text/javascript"> 
    function hello(){ 
    var xx=eval(form1.sum1.value); 
    var yy=eval(form1.sum2.value); 
    form1.result.value=xx+yy 
    } 
    </script>    
     
    
            <script type="text/javascript">
    
    function calcTotal(txtBox, totBox)
    {
        var totVal;
        try
        { 
        totVal = document.getElementById(totBox).value;
        if(totVal!= null && totVal!='')
         {
           document.getElementById(totBox).value= eval(parseInt(document.getElementById(totBox).value) + parseInt(txtBox.value));   
         }
         else
         {
            document.getElementById(totBox).value= txtBox.value;           
         }
        }
        catch(e)
        {}
    }
    </script>

    Comment

    • techker
      New Member
      • Aug 2014
      • 3

      #3
      got the tax..but as soon as i add the code for auto suggest tax stops and auto works??

      Code:
       <script type="text/javascript">
      	$(document).ready(function(){
      	    $('#calx').calx({
      		autocalculate: false
      	    });
      	    
      	    $('#calculate').click(function(){
      		$('#calx').calx('update');
      	    });
      	});
          </script>
          
          
          
          
          <style>
      	input[type="text"]{
      	    width:100%;
      	}
      	td{
      	    padding:5px 10px;
      	}
          </style>
      </head>
      <body>
          <form id="calx">
      	<table>
      	    <thead>
      		<tr>
      		    <td width="268" style="width:250px">Item Name</td>
      		    <td width="162" style="width:80px">Price</td>
      		    <td width="162" style="width:30px">Qty</td>
      		    
      		    <td width="168" style="width:150px">Sub Total</td>
      		</tr>
      	    </thead>
      	    <tbody>
      		<tr>
      		    <td><input id="item1" type="text" name="item[]" value=""/></td>
      		    <td><input id="price1" type="text" name="price[]" value="" data-format="$ 0,0[.]00" /></td>
      		    <td><input id="qty1" type="text" name="qty[]" value="" data-format="0" /></td>
      		   
      		    <td><input id="subtotal1" type="text" name="subtotal[]" value="" data-format="$ 0,0[.]00" data-formula="($price1*$qty1)" /></td>
      		</tr>
      		<tr>
      		    <td><input id="item2" type="text" name="item[]" value="" /></td>
      		    <td><input id="price2" type="text" name="price[]" value="" data-format="$ 0,0[.]00" /></td>
      		    <td><input id="qty2" type="text" name="qty[]" value="" data-format="0" /></td>
      		   
      		    <td><input id="subtotal2" type="text" name="subtotal[]" value="" data-format="$ 0,0[.]00" data-formula="($price2*$qty2)" /></td>
      		</tr>
      		<tr>
      		    <td><input id="item3" type="text" name="item[]" value="" /></td>
      		    <td><input id="price3" type="text" name="price[]" value="" data-format="$ 0,0[.]00" /></td>
      		    <td><input id="qty3" type="text" name="qty[]" value="" data-format="0" /></td>
      		    
      		    <td><input id="subtotal3" type="text" name="subtotal[]" value="" data-format="$ 0,0[.]00" data-formula="($price3*$qty3)" /></td>
      		</tr>
      		<tr>
      		    <td><input id="item4" type="text" name="item[]" value="" /></td>
      		    <td><input id="price4" type="text" name="price[]" value="" data-format="$ 0,0[.]00" /></td>
      		    <td><input id="qty4" type="text" name="qty[]" value="" data-format="0" /></td>
      		    
      		    <td><input id="subtotal4" type="text" name="subtotal[]" value="" data-format="$ 0,0[.]00" data-formula="($price4*$qty4)" /></td>
      		</tr>
      		<tr>
      		    <td><input id="item5" type="text" name="item[]" value="" /></td>
      		    <td><input id="price5" type="text" name="price[]" value="" data-format="$ 0,0[.]00" /></td>
      		    <td><input id="qty5" type="text" name="qty[]" value="" data-format="0" /></td>
      		    
      		    <td><input id="subtotal5" type="text" name="subtotal[]" value="" data-format="$ 0,0[.]00" data-formula="($price5*$qty5)" /></td>
      		</tr>
      		
      		<tr>
      		    <td height="39">&nbsp;</td>
      		    <td>Total Price : </td>
      		    <td colspan="2"><input id="total" type="text" name="total" value="" data-format="$ 0,0[.]00" data-formula="($subtotal1+$subtotal2+$subtotal3+$subtotal4+$subtotal5)" /></td>
      		    
      		</tr>
      		<tr>
      		  <td height="29">&nbsp;</td>
      		  <td>Tax1:</td>
      		  <td colspan="2"><input id="totaltax1" type="text" name="totaltax" value="" data-format="$ 0,0[.]00" data-formula="($total+($total*5.00/100))" /></td>
      		  
      		  </tr>
      		<tr>
      		  <td height="29">&nbsp;</td>
      		  <td>Tax2:</td>
      		  <td colspan="2"><input id="totaltax2" type="text" name="totaltax" value="" data-format="$ 0,0[.]00" data-formula="($total+($total*9.975/100))" /></td>
      		  
      		  </tr>
      		<tr>
      		  <td height="42"><input id="calculate" type="button" value="calculate" /></td>
      		  <td>Tax in :</td>
      		  <td colspan="2"><input id="totaltax" type="text" name="totaltax" value="" data-format="$ 0,0[.]00" data-formula="($total+($total*5.00/100+$total*9.975/100))" /></td>
      		  
      		  </tr>
      	    </tbody>
      	</table>
          </form>

      Comment

      Working...