Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Calculations Form not working

    Hi there,

    Just have a very simple script that im having issues with. I am a newb to Javascript, and cut bits and pieces to form this code.

    Any help in making this work, will be greatly appreciated and good karma will come to you!!!!

    Bascially, I need the output to Add up all the 4 fields.

    Code:
    <html>
    <head>
    <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript">
    <!--
    function assetbank(bank1,bank2,bank3,bank4)
    	{
    	var totalbank = Math.round(bank1 + bank2 + bank3 + bank4);
    	document.calculator.totalassetbank.value = totalbank;
    	}
    //-->
    </SCRIPT>
    </head>
    <body>
    <form name="calculator">
      <p>
        <input type=name="bank1">
    </p>
      <p>
        <input type=name="bank2">
      </p>
      <p>
        <input type=name="bank3">
      </p>
      <p>
        <input type=name="bank4">
    </p>
      <p>
        <input type=button value="Subtotal" onclick="assetbank(document.calculator.bank1.value,document.calculator.bank2.value,document.calculator.bank3.value,document.calculator.bank4.value);">
        <textarea name="totalassetbank"></textarea>
      </p>
    </form>
    </body>
    </html>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    "+" operator has 2 functions, addition and string concatenation. All field values are strings so they will be concatenated and not added. Use Number() method to convert each value to number.
    Code:
    var totalbank = Number(bank1) + Number(bank2) + Number(bank3) + Number(bank4);

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Glenn, thanks so much for having a look for me.

    That makes sense, however I've tried it and it still doesnt work.

    The output is still blank, can you see anything else wrong with the script?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    This is wrong:

    <input type=name="bank1">

    Should be:

    <input type="text" name="bank1">


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •