Forum Moderators: open

Message Too Old, No Replies

Getting info from drop list in html to js function.

         

zero3ree

1:05 am on Nov 8, 2009 (gmt 0)

10+ Year Member



I am trying to add to my loan table by using a drop down list to pick terms of loan. I can not figure how to get the value from the list into the function to calculate it.

2nd part is to send that info back out to html in a div output.I think i can get that part after I am able to get all the values to calulate.

Here is my code

JS:
function amort(balance, interestRate, terms){
var balance = getInt('principal');
var interestRate = getInt('interest');
var terms = dropList("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 sumInterest = 0;
var sumPrinciple = 0;
for (var i = 1; i <= terms; ++i) {
// variable to calculate interest for each month
var thisInterest = balance * monthlyRate;
sumInterest += thisInterest;
// variable to calculate principle after interest is added and payment is subtracted
var thisPrinciple = payment - thisInterest;
sumPrinciple += thisPrinciple;
// variable to calculate new balance after payment
var thisBalance = balance
balance -= thisPrinciple;

result +="<tr><th>" + i + " </th><th>" + thisBalance.toFixed(2) + "</th>" +
"<th>" +
thisInterest.toFixed(2) +
"</th><th>" +
thisPrinciple.toFixed(2) +
"</th>"
"</table>";
}

{
return result;
}
}
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Loan Amortization</title>
<script language="JavaScript" type="text/javascript" src="LoanAmortization.js">
</script>
<script src="div.js" language="JavaScript" type="text/javascript"></script>
<script src="get.js" language="JavaScript" type="text/javascript">
function getInt(id) {
return parseInt(document.getElementById(id).value);
}</script>
<script type="text/javascript">
function dropList()
{
var mylist=document.getElementById("terms");
document.getElementById("terms").value=terms.options[terms.selectedIndex].text;
}
</script>

</head>
<body>
<form>
<fieldset>
<table>

<legend>Inputs</legend>
<table cellpadding="5">
<td><label for="principal">Principal:</label></td>
<td><input type="text" name="principal" id="principal" size="20" value="6000" /></td>
</tr>
<tr>
<td><label for="interest">Interest rate:</label></td>
<td><input type="text" name="interest" id="interest" size="20" value="8.5" /></td>
</tr>
<tr>
<td><label for="amortperiod">Terms:</td>
<td><FORM NAME="mylist">
<SELECT NAME="terms">
<OPTION VALUE="12">12 months
<OPTION VALUE="24">24 months
<OPTION VALUE="36">36 months
<OPTION VALUE="48">48 months
<OPTION VALUE="60">60 months
</SELECT>
</FORM></td>
</tr>
<tr>
<td colspan="2"><input type=button onclick="amort()" value="Calculate!" /></td>
</tr>
</table>
</fieldset>
</form>
</body>

<body>
<form>
<table>
<fieldset>
<legend>Outputs</legend>
<div id="output">
</fieldset>
</table>
</form>
</body>

</html>
The div js is for the second part.
Any guidance is appriciated

daveVk

11:20 pm on Nov 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function dropList(myId)
{
var mylist=document.getElementById(myId);
return mylist.options[mylist.selectedIndex].value;
}

zero3ree

11:41 pm on Nov 8, 2009 (gmt 0)

10+ Year Member



Thank you for the response. I was reposting when you replied I decided to try a different approach but the errors are still stumping me. If you could take a look and let me know if this function is needed for my new method or not,

Thank you.