Cálculo automático de JavaScript

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:

<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;

function subtractone(field) {
  field.value = Number(field.value) - 1;

<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">


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

preguntado el 08 de enero de 11 a las 23:01

2 Respuestas

One problem you've got is that it should be:

<script type="text/javascript">

rather than <script language=...>. Given that fix, the answer suggested by LukeH seems to work fine.

Respondido el 09 de enero de 11 a las 03:01

Don't you just need something like this at the end of your Calc ¿función?

document.form0.total.value = total;
document.form0.divided.value = total / 25;

Respondido el 09 de enero de 11 a las 02:01

thats what I was trying, but that breaks everything. The + and - buttons stop working and nothing calculates at all - JB

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.