Javascript auto calculating

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2011-01-08T23:27:17Z Indexed on 2011/01/08 23:53 UTC
Read the original article Hit count: 146

I have page that automatically calculates a Total by entering digits into the fields or pressing the Plus or Minus buttons.

I need to add a second input after the Total that automatically divides the total by 25. Here is the working code with no JavaScript value for the division part of the code:

<html>
<head>
<script language="text/javascript">
function Calc(className){
var elements = document.getElementsByClassName(className);
var total = 0;

for(var i = 0; i < elements.length; ++i){
total += parseFloat(elements[i].value);
}

document.form0.total.value = total;
}

function addone(field) {
  field.value = Number(field.value) + 1;
  Calc('add');
}

function subtractone(field) {
  field.value = Number(field.value) - 1;
  Calc('add');
}
</script>

</head>
<body>
<form name="form0" id="form0">
1: <input type="text" name="box1" id="box1" class="add" value="0" onKeyUp="Calc('add')" onChange="updatesum()" onClick="this.focus();this.select();" />
<input type="button" value=" + " onclick="addone(box1);">
<input type="button" value=" - " onclick="subtractone(box1);">
<br />

2: <input type="text" name="box2" id="box2" class="add" value="0" onKeyUp="Calc('add')" onClick="this.focus();this.select();" />
<input type="button" value=" + " onclick="addone(box2);">
<input type="button" value=" - " onclick="subtractone(box2);">
<br />

3: <input type="text" name="box3" id="box3" class="add" value="0" onKeyUp="Calc('add')" onClick="this.focus();this.select();" />
<input type="button" value=" + " onclick="addone(box3);">
<input type="button" value=" - " onclick="subtractone(box3);">
<br />

<br />
Total: <input readonly style="border:0px; font-size:14; color:red;" id="total" name="total">
<br />
Totaly Divided by 25: <input readonly style="border:0px; font-size:14; color:red;" id="divided" name="divided">

</form>
</body></html>

I have the right details but the formulas I am trying completely break other aspects of the code. I cant figure out how to make the auto adding and auto dividing work at the same time

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about calculation