1 Reply Latest reply on Oct 1, 2007 4:45 PM by cf_dev2

    adding using javascript

    tapping Level 1
      I cannot get the following Javascript calculation to work. Could someone give me an idea of what the problem might be? The code is below:

      <head>
      <title>Account Calculator</title>
      <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
      function calculate(){

      var cost = document.calculate.costoftreatment.value;
      var inspayment = document.calculate.insurancepayment.value;
      var dwnpayment = document.calculate.downpayment.value;

      var total = cost + inspayment;
      document.calculate.amounttobefinanced.value = total;

      }
      </script>
      </head>

      <body>

      <table width="43%" border="0" align="center" bgcolor="#CCCCCC">

      <tr>
      <td height="45" colspan="2" ><div align="center" class="style4">
      <p>Account Calculator </p>
      </div></td>
      </tr>

      <tr>
      <td ><div align="right"><span class="style3">Cost of Treatment </span></div></td>
      <td >
      <form name = "calculate" >
      <div align="center"> <input type="text" NAME="costoftreatment" id="costoftreatment" onKeyPress="if (event.keyCode != 46 && event.keyCode < 48 || event.keyCode > 57 ) event.returnValue = false;" onChange="calculate();" ></div>
      </td>
      </tr>
      <tr>
      <td><div align="right"><span class="style3">Insurance Estimate</span> </div></td>
      <td >
      <div align="center"> <input name="insurancepayment" type="text" id="insurancepayment" onchange="calculate();" ></div>
      </td>
      </tr>
      <tr>
      <td><div align="right"><span class="style3">Down Payment </span></div></td>
      <td><div align="center">
      <input name="downpayment" type="text" id="downpayment" onchange="calculate();" >
      </div></td>
      </tr>
      <tr>
      <td><div align="right"><span class="style3">Amount to be Financed </span></div></td>
      <td><div align="center">
      <input name="amounttobefinanced" type="text" id="amounttobefinanced" onChange="calculate();" >
      </div></td>
      </tr>
      </form>
      </table>
        • 1. Re: adding using javascript
          cf_dev2 Level 1
          Your form and function both have the same name: calculate. Perhaps that's confusing the browser. If you're using Firefox it has a javascript error console that's helpful in debugging javascript problems. Tools > Error Console.

          > var total = cost + inspayment;
          The values entered in the text boxes might not be numbers. So that line would cause an error. The function should anticipate /handle that.