Here is an example :
Html code :
<div>
Addition 1:<br />
<input type="text" id="add1" name="add1" onkeyup="AutoCalc(this)" />
<br />
Subtraction 1:<br />
<input type="text" id="sub1" name="sub1" onkeyup="AutoCalc(this)" />
<br />
Addition 2:<br />
<input type="text" id="add2" name="add2" onkeyup="AutoCalc(this)" />
<br />
Subtraction 2:<br />
<input type="text" id="sub2" name="sub2" onkeyup="AutoCalc(this)" />
<br />
<br />
<label>
Total</label>
<input type="text" id="total" name="total" disabled="disabled" />
</div>
And Javascript :
function AutoCalc(obj) {
var total = 0;
if (isNaN(obj.value)) {
alert("Please enter a number :(");
obj.value = '';
return false;
}
else {
var textBox = new Array();
textBox = document.getElementsByTagName('input')
for (i = 0; i < textBox.length; i++) {
if (textBox[i].type == 'text') {
var inputVal = textBox[i].value;
if (inputVal == '')
inputVal = 0;
if ((textBox[i].id == 'add1') || (textBox[i].id == 'add2')) {
total = total + parseInt(inputVal);
}
if ((textBox[i].id == 'sub1') || (textBox[i].id == 'sub2')) {
total = total - parseInt(inputVal);
}
}
}
document.getElementById('total').value = total;
}
}
Hope it helps you.