Skip to content

Instantly share code, notes, and snippets.

@mleon
Created September 21, 2009 15:57
Show Gist options
  • Save mleon/190338 to your computer and use it in GitHub Desktop.
Save mleon/190338 to your computer and use it in GitHub Desktop.
<h2>JQuery Mortgage Calculator</h2>
<script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js'></script>
<!--The following formula is used to calculate the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of c. [If the quoted rate is 6%, for example, c is .06/12 or .005]. P = L[c(1 + c)^n]/[(1 + c)^n - 1]-->
<h3>Mortgage Calculator Example<br /></h3>
<form>
<p><input id="mcPrice" class="mortgageField" name="mcPrice" type="text" /> Sale price ($)</p>
<p><input id="mcDown" class="mortgageField" name="mcDown" type="text" /> Down payment (%)</p>
<p><input id="mcRate" class="mortgageField" name="mcRate" type="text" /> Interest Rate (%)</p>
<p><input id="mcTerm" class="mortgageField" name="mcTerm" type="text" /> Term (years)</p>
<button id="mortgageCalc" class="smallButton" onclick="return false">Calculate Monthly Payment</button>
<br />
<p><input id="mcPayment" class="mortgageAnswer" name="mcPayment" type="text" /> Monthly Payment</p>
<p><input id="mcInterestPayment" class="mortgageAnswer" name="mcInterestPayment" type="text" /> Initial Interest Payment</p>
</form>
<p>
<script type="text/javascript"><!--
$("#mortgageCalc").click(function(){
var L,P,n,c,dp;
L = parseInt($("#mcPrice").val());
n = parseInt($("#mcTerm").val()) * 12;
c = parseFloat($("#mcRate").val())/1200;
dp = 1 - parseFloat($("#mcDown").val())/100;
L = L * dp;
P = (L*(c*Math.pow(1+c,n)))/(Math.pow(1+c,n)-1);
I = L*c;
if(!isNaN(I))
$("#mcInterestPayment").val(I.toFixed(2));
else
$("#mcInterestPayment").val('There was an error');
if(!isNaN(P))
$("#mcPayment").val(P.toFixed(2));
else
$("#mcPayment").val('There was an error');
return false;
});
// --></script>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment