Jump to content

Can't get my javascript to work

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Hello i'm currently trying to develop a loan calculator which lets the user type in a loan amount, loan repyament per month and an interest rate. When the user clicks on the button i want it to display the term of the loan in years and months and the total interest to be paid.


<html>

  <head>

    <title>Calculate Loan</title>

    <style type="text/css">

      body {

             background-color: #000000;

             color: #FFFFFF;

             font-size: 30;

             text-align: center;

           }

    </style>

    <script type="text/javascript">

    function calculate()

      {

        term = 0;

        totalInterest = 0;

        var amount, repayment, rate, monthInterest, term, totalInterest = parseFloat(document.frmLoan.txtLoanAmount.value) * parseFloat(document.frmrate.txtMonthlyRepayment.value), outstring; 

        outstring = "$" + document.frmrate.txtRate.value + " payrate and " + document.frmrate.txtHrs.value + " hour(s) ";

        outstring = outstring + " means you earned $" + sum;

        document.getElementById('div1').innerHTML = outstring;

      }


    function clean()

       {

         //alert("in clean");

         document.frmLoan.txtLoanAmount.value = "";

         document.frmLoan.txtMonthlyRepayment.value = "";

                 document.frmLoan.txtInterestRate.value = "";

         document.getElementById('div1').innerHTML = "";

         document.frmLoan.txtLoanAmount.focus();

         document.frmLoan.txtMonthlyRepayment.focus();

         document.frmLoan.txtLoanAmount.focus();

                 document.frmLoan.txtLoanAmount.focus();

       }

    </script>

    </head>

    <body onload="window.document.frmLoan.txtLoanAmount.focus()">


    <h1>Calculate Loan</h1>

  <form name="frmLoan">

  Enter loan amount : <input type="text" name="txtLoanAmount">

    <br /><br />

  Enter loan repayment amount per month : <input type="text" name="txtMonthlyRepayment">

    <br /><br />

  Enter interest rate : <input type="text" name="txtInterestRate">


    <br /><br />

  

  </form>

   <br /> <br />

   <input type="button" name="cmdcalculate" value="Calculate term of loan and total interest" onclick="calculate()" />

      

   <input type="button" name="cmdClear" value="Reset" onClick="clean()">

   <br /><br /><br />

    <div id="div1"></div>

    </body>

   </html>


That is my code so far i'm not sure what i'm doing wrong but if anyone could help me that would be great. Cheers.

#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
You have some problems in the calculate function. You use document.frmrate instead of document.frmLoan. txtRate should be txtInterestRate, and txtHrs is not defined anywhere. sum is also not defined.

It seems that you have rewritten or cut&paste the code but forgotten to change some variable names.

#3
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
you are missing a lotta code in the above document ( think you stripped it off :D) any way here is a simple version you get the 3 variables needed just do with the calculations,

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Calculate Loan</title>
    <style type="text/css">
      body {
             background-color: #000000;
             color: #FFFFFF;
             font-size: 30;
             text-align: center;
           }
    </style>

    <script type="text/javascript">
    function calculate()
      {
      var _Amt = parseFloat(document.getElementById('txtLoanAmmt').value);
      var _Term = parseFloat(document.getElementById('txtMonthly').value);
      var _Interest = parseFloat(document.getElementById('txtInterest').value);

      document.getElementById('div1').innerHTML = "Loan Term is: "+_Amt/_Term);
      }

    function clean()
       {
          document.getElementById('txtLoanAmmt').value="";
         document.getElementById('txtMonthly').value="";
         document.getElementById('txtInterest').value="";
         document.getElementById('div1').innerHTML="";
       }
    </script>

</head>
<body>
    <h1>
        Calculate Loan</h1>
    <h5>
        loan amount :</h5>
    <input id="txtLoanAmmt" type="text" />
    <br />
    <br />
    <h5>
        loan repayment amount per month :</h5>
    <input id="txtMonthly" type="text" />
    <br />
    <br />
    <h5>
        Enter interest rate :</h5>
    <input id="txtInterest" type="text" />
    <br />
    <br />
    <br />
    <br />
    <input type="button" value="Calculate term of loan and total interest" onclick="calculate()" />
    <input type="button" value="Reset" onclick="clean()" />
    <br />
    <br />
    <br />
    <div id="div1">
    </div>
</body>
</html>

Edited by gokuajmes, 20 September 2010 - 02:03 AM.
cleaned the code