SHARE
TWEET

Summing 8 values

Mike_T Aug 16th, 2013 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.  
  3. (function(){
  4.  
  5. function addListener(element, eventName, handler) {
  6.   if (element.addEventListener) {
  7.     element.addEventListener(eventName, handler, false);
  8.   }
  9.   else if (element.attachEvent) {
  10.     element.attachEvent('on' + eventName, handler);
  11.   }
  12.   else {
  13.     element['on' + eventName] = handler;
  14.   }
  15. }
  16.  
  17. var amount1  = document.getElementById("input_47"),
  18.     amount2  = document.getElementById("input_45"),
  19.     amount3  = document.getElementById("input_43"),
  20.     amount4  = document.getElementById("input_41"),
  21.     amount5  = document.getElementById("input_39"),
  22.     amount6  = document.getElementById("input_37"),
  23.     amount7  = document.getElementById("input_35"),
  24.     amount8  = document.getElementById("input_33"),
  25.     total    = document.getElementById("input_50");
  26.  
  27. function summing () {
  28.   total.value = (Number(amount1.value)) +
  29.                 (Number(amount2.value)) +
  30.                 (Number(amount3.value)) +
  31.                 (Number(amount4.value)) +
  32.                 (Number(amount5.value)) +
  33.                 (Number(amount6.value)) +
  34.                 (Number(amount7.value)) +
  35.                 (Number(amount8.value));
  36.   total.disabled = true;
  37. }
  38.  
  39. function updateTotalFormat()
  40. {
  41.   var currencyTotal = new String();
  42.       currencyTotal = formatCurrency(total.value);
  43.       total.value   = currencyTotal;
  44. }
  45.  
  46. addListener (amount1, 'change', function () {
  47.   summing();
  48.   updateTotalFormat();
  49. });
  50. addListener (amount2, 'change', function () {
  51.   summing();
  52.   updateTotalFormat();
  53. });
  54. addListener (amount3, 'change', function () {
  55.   summing();
  56.   updateTotalFormat();
  57. });
  58. addListener (amount4, 'change', function () {
  59.   summing();
  60.   updateTotalFormat();
  61. });
  62. addListener (amount5, 'change', function () {
  63.   summing();
  64.   updateTotalFormat();
  65. });
  66. addListener (amount6, 'change', function () {
  67.   summing();
  68.   updateTotalFormat();
  69. });
  70. addListener (amount7, 'change', function () {
  71.   summing();
  72.   updateTotalFormat();
  73. });
  74. addListener (amount8, 'change', function () {
  75.   summing();
  76.   updateTotalFormat();
  77. });
  78.  
  79. // Currency format
  80. var decimalCharacter = Number("1.1").toLocaleString().substr(1,1);
  81. var defaultCurrencyMarker = "";
  82. function formatCurrency(number, currencyMarker) {
  83.     if (typeof number != "number")
  84.         number = parseFloat(number, 10);
  85.  
  86.     // if NaN is passed in or comes from the parseFloat, set it to 0.
  87.     if (isNaN(number))
  88.         number = 0;
  89.  
  90.     var sign = number < 0 ? "-" : "";
  91.     number = Math.abs(number);  // so our signage goes before the $ symbol.
  92.  
  93.     var integral = Math.floor(number);
  94.     var formattedIntegral = integral.toLocaleString();
  95.  
  96.     // IE returns "##.00" while others return "##"
  97.     formattedIntegral = formattedIntegral.split(decimalCharacter)[0];
  98.  
  99.     var decimal = Math.round((number - integral) * 100);
  100.     return sign + (currencyMarker || defaultCurrencyMarker) +
  101.         formattedIntegral  +
  102.         decimalCharacter +
  103.         decimal.toString() + (decimal < 10 ? "0" : "");
  104. }
  105.  
  106. })();
  107.  
  108. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top