It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Forms - Beginner question

Forms - Beginner question

Postby JamesDE on Sun Jun 22, 2008 5:12 pm

I'm trying to create an amortization table with text boxes as input fields and I'd like to output the table to the same page. I have the input part working but can't get the table to output. Thank you.

HTML:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Loan Amortization</title>
<style type="text/css">
body {background-color: #CCCCCC}
</style>
<script type="text/javascript" src="loanamortization.js"></script>
</head>
<body>
<form id="loanAmortization" name="loanAmortization">
<fieldset>
<legend>Inputs</legend>
<label for="principal">Principal</label><br />
<input type="text" name="principal" id="principal" value="10000" /><br />

<label for="interest">Interest</label><br />
<input type="text" name="interest" id="interest" value=".05" /><br />

<label for="terms">Terms</label><br />
<select name="terms" id="terms">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
</select><br /><br />
<input type="submit" value="Calculate" onclick="calculate()" /><br />
</fieldset>
<DIV id="output">
</DIV>
</form>
</body>



JS:

function calculate()
{
var principal = parseFloat(document.loanAmortization.principal.value);

var interest = parseFloat(document.loanAmortization.interest.value);
var terms = parseInt(document.loanAmortization.terms.value);
document.loanAmortization.output.value = amort(principal, interest, terms);

}

function amort(balance, interestRate, terms)
{
var monthlyRate = interestRate/12;
var payment = balance * (monthlyRate/(1-Math.pow(
1+monthlyRate, -terms)));

var result = "Loan amount: $" + balance.toFixed(2) + "<br />" +
"Interest rate: " + (interestRate*100).toFixed(2) + "%<br />" +
"Number of months: " + terms + "<br />" +
"Monthly payment: $" + payment.toFixed(2) + "<br />" +
"Total paid: $" + (payment * terms).toFixed(2) + "<br /><br />";

result += "<table border='1'><tr><th>Month</th><th>Balance</th>" +
"<th>Interest</th><th>Principal</th>";

var interest = 0;
var principal = 0;
for (var i = 1; i <= terms; i++)
{
interest = balance * monthlyRate;
principal = payment - interest;
result += "<tr><td>" + i + "</td>" + "<td>" + balance.toFixed(2) +
"</td>" + "<td>" + interest.toFixed(2) + "</td>" + "<td>" +
principal.toFixed(2) + "</td></tr>";
balance = balance - principal;
}
result += "</table>";
return result;
}
JamesDE
 
Posts: 2
Joined: Sun Jun 22, 2008 5:07 pm

Postby rangana on Tue Jun 24, 2008 2:39 am

You are referencing to an id and not an element of a form, so this part is erroneous:
Code: Select all
document.loanAmortization.output.value


You need to change it to:
Code: Select all
document.getElementById('output').innerHTML


Hope it helps. ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 7 guests