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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript calculation problem

    I can't figure this out. Sometimes a second set of eyes is helpful, so I figured I'd see what you guys think.

    The purpose of this is to take five numbers, generate tax, choose a shipping method, and then finally combine tax, cost, and shipping.

    The last step is what I can't get working.

    Here's my code... any help would be appreciated.
    PS: I put asterisks around the bit of code that I can't get working... for easy reference.


    Code:
    <html>
    
    <head>
    
    <script>
    function runTotal() {
    
    document.myform.total.value =(eval(document.myform.field1.value)
          + eval(document.myform.field2.value)
          + eval(document.myform.field3.value)
          + eval(document.myform.field4.value)
          + eval(document.myform.field5.value)).toFixed(2);
    Tax = (document.myform.total.value * .06 ).toFixed(2);
    document.myform.tax.value = Tax;
    
    ship = eval(document.myform.ship.value);
    cost = eval(document.myform.total.value);
    ***document.myform.grandtotal.value = eval(ship + Tax + cost).toFixed(2);***
    
    }
    
    
    </script>
    </head>
    <body>
    <form name="myform">
    
    <input type ='text' name = 'field1' value="0.00"> <br />
    <input type ='text' name = 'field2' value="0.00"> <br />
    <input type ='text' name = 'field3' value="0.00"> <br />
    <input type ='text' name = 'field4' value="0.00"> <br />
    <input type ='text' name = 'field5'value="0.00"> <br />
    <input type ='text' name = 'total' readonly>Total <br />
    <input type ='text' name = 'tax'>6% <br />
    <input type="radio" name = 'ship' value="25" checked onClick="document.myform.shipping.value = 25.00"> Next Day <br />
    <input type="radio" name = 'ship' value="10" onClick="document.myform.shipping.value = 10.00"> USPS <br />
    <input type="radio" name = 'ship' value="5"  onClick="document.myform.shipping.value = 5.00">  Mule<br />
    <input type="text" name = "shipping" value="25">Shipping <br />
    
    
    <input type ='text' name = 'grandtotal'>Grand Total <br />
    
    <input type= "button" value= "Calculate Total"
     onClick= "runTotal();">
    
    
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this

    Code:
    function runTotal() {
      document.myform.total.value = parseFloat(document.myform.field1.value)
          + parseFloat(document.myform.field2.value)
          + parseFloat(document.myform.field3.value)
          + parseFloat(document.myform.field4.value)
          + parseFloat(document.myform.field5.value);
      Tax = parseFloat(document.myform.total.value) * .06;
      document.myform.tax.value = Tax.toFixed(2);
    
      ship = parseFloat(document.myform.shipping.value);
      cost = parseFloat(document.myform.total.value);
      document.myform.grandtotal.value = (ship + Tax + cost).toFixed(2);
    }
    david_kw

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah, that works flawlessly. Thanks


  •  

    Posting Permissions

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