Top » JAVASCRIPTS » TOTAL AND AVERAGE INPUT NUMBERS If you find this page useful
please make a secure donation
My Account  |  Cart Contents  |  Checkout   
Javascript Category --> MATH  DESCRIPTION - RUN CODE

AD
AD


This is the Code View for Total and Average Input Numbers :

Go Get The Code Now - See The Code Run

<html>
<head>
<!--
This file retrieved from the JS-Examples archives
http://www.js-examples.com
1000s of free ready to use scripts, tutorials, forums.
Author: JS-Examples - http://www.js-examples.com/
-->

</head>
<body>
<form name=f1>
Box 1<input onblur=addTotals(1) type=text name=item1 value=4>
<BR>
Box 2<input onblur=addTotals(1) type=text name=item2 value=7>
<BR>
Total (1 and 2):
<input type=text name=total_1 value="?" readonly>
<BR>Average (1 and 2):
<input type=text name=avg_1 value="?" readonly>
<HR>
Box 3<input onblur=addTotals(2) type=text name=item3 value=4>
<BR>
Box 4<input onblur=addTotals(2) type=text name=item4 value=7>
<BR>
Total (3 and 4):
<input type=text name=total_2 value="?" readonly>
<BR>Average (3 and 4):
<input type=text name=avg_2 value="?" readonly>
</form>
<script language="javascript"> 
function addTotals(_v) { 
  with (document.forms["f1"])
  {
    var totalResult=0;
    if(_v==1)
	{
      totalResult = Number( item1.value ) + Number( item2.value ); 
      avg_1.value = roundTo( totalResult / 2, 2 );
      total_1.value = roundTo( totalResult, 2 );
	}
    if(_v==2)
	{
      totalResult = Number( item3.value ) + Number( item4.value ); 
      avg_2.value = roundTo( totalResult / 2, 2 );
      total_2.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>

<BR><center><a href='http://www.js-examples.com'>JS-Examples.com</a></center>
</body>
</html>



This script has not yet been rated -- be the first...

Author Name:JS-Examples   [web site
Date Submitted:August, 2002
Times Viewed:17,706
Runs in IE:Yes
Runs in NS:Yes
Category List:
MATH
Key Words:
math add total average