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 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2006
    Location
    California
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to create a form that calculates 3 fields...

    I want to have two fields. You put in a number (ie 10) then in the next field you put another number (ie 15) then it multiplies those numbers together, and then mulitplies the result by .75

    anything out there already? or something that can be modified? i'm not a JS expert, so some assistance might be needed if we can find something that will work..'

    Thanks!

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    908
    Thanks
    0
    Thanked 120 Times in 119 Posts
    javascript will allways try to convert strings to numbers when multiplying and dividing.
    so in this case you can just say:
    Result = field1.value * field2.value * .75;

    whereas if you want to add or substract,
    you will have to convert the strings to numbers before the calculation.
    It can be done by multiplying the strings by 1 .e.g.:
    Result = (field1.value * 1) + (field2.value * 1);.

    need more help? just ask!

  • #3
    New Coder
    Join Date
    Jan 2006
    Location
    California
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but i'm a total noob at JS... I can't really figure it out by the line of code you gave...

    Could you use it in an example form?

    Thanks a million!!

  • #4
    New Coder
    Join Date
    Jan 2006
    Location
    California
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, nevermind. I figured it out!! Thanks!!

  • #5
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Code:
    <html>
    <head>
    <script language="javascript">
    
    function doMath(valueA, valueB, multiplier){
      return (valueA*valueB*multiplier);
    }
    
    </script>
    </head>
    <body>
    
    <form onsubmit="alert(doMath(this.boxA.value, this.boxB.value, .75));">
    <input type="text" name="boxA"><br>
    <input type="text" name="boxB"><br>
    <input type="submit">
    </form>
    </body>
    </html>


  •  

    Posting Permissions

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