SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Calculation onload

    I currently have a web page that is building a web form from data contained in a SQL table. I need to do some calculations on the numbers that are populated into the form. I have used the following javascript code to successfully complete my calculations but I can only use this code at the bottom of my page because executing it at the top of my page the form fields are still empty. How would I use or modify the code I have to display the calc at the top of the web form. Example as follows:

    <form method="POST" target="_self" name="dialer" enctype="text/plain">

    Loan Amount: <input type="text" value="400000" name="loan" />
    Interest Rate: <input type="text" value="1.00" name="apr" />
    Amortization Term: <input type="text" value="480" name="term" />

    </form>

    <script language="JavaScript">

    var princ = document.dialer.loan.value;
    var term = document.dialer.term.value;
    var intr = document.dialer.apr.value / 1200;

    var output = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
    var calc = (Math.round(output * 100)/100);

    </script>

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The language attribute is deprecated. Use the type attribute instead.

    You can use the onload event for this. (If you want multiple onload handlers, then you'll need more code.)
    Code:
    <script type="text/javascript">
    window.onload = function(){
      var princ = document.dialer.loan.value;
      var term = document.dialer.term.value;
      var intr = document.dialer.apr.value / 1200;
      
      var output = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
      var calc = (Math.round(output * 100)/100);
    }
    </script>
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

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