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 to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question textbox enter dollar value, update another with paypal charge included

    Looking for an example of how to take the value typed in one textbox to be calculated and update the value of a second texbox on the fly.

    The calculation is,

    (2.9% X first textbox value) + $0.30 = second textbox value

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    I take it that the value of the first textbox actually is a number (so no characters except digits and a maximum of one dot)?

    If that is the case
    Code:
    var firstvalue = parseFloat(document.getElementById('idOfFirstTextbox').value);
    document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);

  • Users who have thanked devnull69 for this post:

    idunnojs (02-26-2012)

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Correct

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation

    Quote Originally Posted by devnull69 View Post
    I take it that the value of the first textbox actually is a number (so no characters except digits and a maximum of one dot)?

    If that is the case
    Code:
    var firstvalue = parseFloat(document.getElementById('idOfFirstTextbox').value);
    document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="application/javascript">
    var firstvalue = parseFloat(document.getElementById('idOfFirstTextbox').value);
    document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);</script>
    </head>
    
    <body>
    <form>
      <input id="idOfFirstTextbox" name="idOfFirstTextbox" type="text" value="" />
      <input id="idOfSecondTextbox" name="idOfSecondTextbox" type="text" value="" />
    </form>
    </body>
    </html>
    This doesn't appear to work or did I misunderstand implementing this?

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Yes you did

    Before implementing code you should ask yourself what exactly(!) you want to have. Observe your code top-down and you will find out that your code says:

    Immediately (before even processing the HTML below) get the value of the first textbox (which doesn't exist yet) and calculate a result into the second textbox. Do this only once, whatever happens.

    What you wanted to do is something similar to:

    After those two textboxes have been loaded, listen for every change of the first textbox. On every change take the (numerical) input of this textbox and calculate a result into the second textbox

    Code:
    <script type="text/javascript">
    window.onload = function() {
       document.getElementById('idOfFirstTextbox').onchange = function() {
          var firstvalue = parseFloat(this.value);
          document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);
       }
    }
    </script>


  •  

    Tags for this Thread

    Posting Permissions

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