INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Add numeric values together

How do I add/subtract numeric values dynamically without hitting the 'Submit' button? (version 2) by GUJUm0deL
Posted: 11 May 07

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

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close