Total Up Form

[Total: 0    Average: 0/5]

This will total up two form text boxes.

Body Code:

<form name=f1>
Box 1<input onblur=addTotals() type=text name=one value=4>
<BR>
Box 2<input onblur=addTotals() type=text name=two value=7>
<BR>
Total:
<input type=text name=total value="?" readonly>
</form>
<script language="javascript"> 
function addTotals() { 
  with (document.forms["f1"])
  {
    var totalResult = Number( one.value ) + Number( two.value ); 
    total.value = roundTo( totalResult, 2 ); 
  }
} 


function roundTo(num,pow){ 
  if( isNaN( num ) )
  { 
    num = 0; 
  } 

  num *= Math.pow(10,pow); 
  num = (Math.round(num)/Math.pow(10,pow))+ "" ; 
  if(num.indexOf(".") == -1) 
    num += "." ; 
  while(num.length - num.indexOf(".") - 1 < pow) 
    num += "0" ; 

  return num; 
} 



</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *