• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

adding using javascript

New Here ,
Oct 01, 2007 Oct 01, 2007

Copy link to clipboard

Copied

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>
TOPICS
Advanced techniques

Views

241

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 01, 2007 Oct 01, 2007

Copy link to clipboard

Copied

LATEST
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation