Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I add/subtract numeric values dynamically without hitting the 'Submit' button? (version 2)

Add numeric values together

How do I add/subtract numeric values dynamically without hitting the 'Submit' button? (version 2)

by  GUJUm0deL  Posted    (Edited  )
<script language="javascript" type="text/javascript">
function addthem() {
//ADD FIRST INPUT VALUE
var add1 = document.addem.input1.value
var add1 = parseFloat(add1, 10)
add1 = (isNaN(add1))?0:add1;

//ADD SECOND INPUT VALUE
var add2 = document.addem.input2.value
var add2 = parseFloat(add2, 10)
add2 = (isNaN(add2))?0:add2;

//ADD THEM TOGETHER
return eval(add1) + eval(add2);
}

function subtractthem() {
//SUBTRACT FIRST INPUT VALUE
var add01 = document.addem.input01.value
var add01 = parseFloat(add01, 10)
add01 = (isNaN(add01))?0:add01;

//SUBTRACT SECOND INPUT VALUE
var add02 = document.addem.input02.value
var add02 = parseFloat(add02, 10)
add02 = (isNaN(add02))?0:add02;

//SUBTRACT THEM TOGETHER
return eval(add01) + eval(add02);
}

//FUNCTION WILL PARSE THE TOTAL AS DOLLAR FORMAT
function dollarformat(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) num = "0";
cents = Math.floor((num*100+0.5)%100);
num = Math.floor((num*100+0.5)/100).toString();
if(cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
return ('$' + num + '.' + cents);
}

//CALCUATES THE ADD/SUBTRACT VALUES
function calculate() {
var x = addthem()
var y = subtractthem()
if(x>y) {
max = x+y
document.addem.total.value = dollarformat(max)
}
if(x<y) {
max = dollarformat(y-x)
max = "-" + max
document.addem.total.value = max
}
if (x==y) {
max = x-y
document.addem.total.value = dollarformat(max)
}
}

function addition() {
document.addem.answer.value = addthem()
document.addem.answer.value = dollarformat(document.addem.answer.value)
calculate()
}

function subtration() {
document.addem.answer2.value = subtractthem()
document.addem.answer2.value = dollarformat(document.addem.answer2.value)
calculate()
}

function calculating() {
document.addem.total.value = calculate()
document.addem.total.value = dollarformat(document.addem.total.value)
}
</script>

<form name="addem">
<table border="0" cellpadding="2">
<tr>
<td colspan="3">
<p>Enter a number in the 'addend' section and have the positive numbers add up. Enter a number in the 'subend' section and have the negative numbers added together. &nbsp;The final numbers will added in the 'total' section. This script is very useful for adding and subtracting numbers together.</p>
</td>
</tr>
<tr>
<td valign="middle">addend: <input type="TEXT" name="input1" onChange="addition(addem)" size="20" maxlength="10"></td>
<td valign="middle">subend: <input type="TEXT" name="input01" onChange="subtration(addem)" size="20" maxlength="10"></td>
</tr>
<tr>
<td valign="middle">addend: <input type="TEXT" name="input2" onChange="addition(addem)" size="20" maxlength="10"></td>
<td valign="middle">subend: <input type="TEXT" name="input02" onChange="subtration(addem)" size="20" maxlength="10"></td>
</tr>
<tr>
<td height="82" valign="middle">answer: <input type="TEXT" name="answer" onFocus="this.blur();" size="20" maxlength="10"></td>
<td height="82" valign="middle">subanw: <input type="TEXT" name="answer2" onFocus="this.blur();" size="20" maxlength="10"></td>
<td height="82" valign="middle">total: <input type="TEXT" name="total" onFocus="this.blur();" size="20" maxlength="10"></td>
</tr>
</table>
</form>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top