<html>
<body>
<form action="#" method="post">
Addition 1:
<input type="text" id="add1" name="add1" onkeyup="AutoCalc(this)" />
Subtraction 1:
<input type="text" id="sub1" name="sub1" onkeyup="AutoCalc(this)" />
Addition 2:
<input type="text" id="add2" name="add1" onkeyup="AutoCalc(this)" />
Subtraction 2:
<input type="text" id="sub2" name="sub1" onkeyup="AutoCalc(this)" />
<label>
Total</label>
<input type="text" id="total" name="total" disabled="disabled" />
</div>
</body>
</html>
<script>
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;
}
}
</script>
abim usman 0 Newbie Poster
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.