function calculateMortgage() {
// Retrieve inputs
var homePrice = parseFloat(document.getElementById("mc_homePrice").value);
var downPayment = parseFloat(document.getElementById("mc_downPayment").value);
var interestRate = parseFloat(document.getElementById("mc_interestRate").value);
var loanTermYears = parseFloat(document.getElementById("mc_loanTerm").value);
var annualTax = parseFloat(document.getElementById("mc_propertyTax").value);
var annualInsurance = parseFloat(document.getElementById("mc_homeInsurance").value);
// Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTermYears) || isNaN(annualTax) || isNaN(annualInsurance)) {
alert("Please enter valid numbers in all fields.");
return;
}
if (downPayment >= homePrice) {
alert("Down payment cannot be greater than or equal to the home price.");
return;
}
// Calculations
var loanAmount = homePrice – downPayment;
var monthlyInterestRate = (interestRate / 100) / 12;
var numberOfPayments = loanTermYears * 12;
// Principal and Interest (Amortization Formula)
// M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
var monthlyPI = 0;
if (monthlyInterestRate === 0) {
monthlyPI = loanAmount / numberOfPayments;
} else {
var numerator = monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments);
var denominator = Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1;
monthlyPI = loanAmount * (numerator / denominator);
}
// Taxes and Insurance
var monthlyTax = annualTax / 12;
var monthlyInsurance = annualInsurance / 12;
// Totals
var totalMonthlyPayment = monthlyPI + monthlyTax + monthlyInsurance;
var totalCostOfLoan = monthlyPI * numberOfPayments;
var totalInterestPaid = totalCostOfLoan – loanAmount;
// Payoff Date
var today = new Date();
var payoffDate = new Date(today.setMonth(today.getMonth() + numberOfPayments));
var options = { month: 'long', year: 'numeric' };
var payoffString = payoffDate.toLocaleDateString("en-US", options);
// Formatting Helpers
function formatMoney(amount) {
return "$" + amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
// Update DOM
document.getElementById("mc_displayTotalMonthly").innerHTML = formatMoney(totalMonthlyPayment);
document.getElementById("mc_displayPI").innerHTML = formatMoney(monthlyPI);
document.getElementById("mc_displayTax").innerHTML = formatMoney(monthlyTax);
document.getElementById("mc_displayIns").innerHTML = formatMoney(monthlyInsurance);
document.getElementById("mc_displayLoanAmount").innerHTML = formatMoney(loanAmount);
document.getElementById("mc_displayTotalInterest").innerHTML = formatMoney(totalInterestPaid);
document.getElementById("mc_displayPayoffDate").innerHTML = payoffString;
// Show Results
document.getElementById("mc_resultSection").style.display = "block";
}
Understanding Your Mortgage Calculation
Buying a home is one of the largest financial decisions most people make in their lifetime. A robust Mortgage Calculator is essential for understanding not just your monthly financial obligation, but the long-term cost of borrowing money. This tool breaks down your payments into principal, interest, taxes, and insurance (often referred to as PITI).
The Components of Your Monthly Payment
When you write a check for your mortgage, it usually covers more than just the loan repayment. Here is what constitutes a typical payment:
Principal: This is the portion of your payment that goes directly toward reducing the loan balance. In the early years of a mortgage, this amount is small, but it grows over time as you pay down the debt.
Interest: This is the cost of borrowing money. With a fixed-rate mortgage, your interest rate stays the same, but the amount of interest you pay in dollars decreases every month as your principal balance shrinks.
Property Taxes: Local governments assess taxes on real estate to fund public services. Lenders often collect this monthly and hold it in an escrow account to pay the tax bill when it's due.
Homeowners Insurance: This protects your property against damage. Like taxes, this is often paid through an escrow account managed by your lender.
How Interest Rates Affect Buying Power
Even a small difference in interest rates can have a massive impact on your monthly payment and the total interest paid over the life of the loan. For example, on a $300,000 loan, a 1% increase in interest rate can increase your monthly payment by hundreds of dollars and your total interest cost by tens of thousands of dollars.
The Role of the Down Payment
Your down payment represents your initial equity stake in the property. A larger down payment reduces the loan amount (principal), which in turn lowers your monthly principal and interest payments. Additionally, putting down at least 20% often allows you to avoid Private Mortgage Insurance (PMI), a separate fee that protects the lender if you default.
Amortization Explained
Amortization is the process of spreading out a loan into a series of fixed payments over time. While your total monthly payment remains constant with a fixed-rate mortgage, the ratio of principal to interest changes. At the beginning of your loan term (e.g., year 1 of 30), the majority of your payment goes toward interest. By the end of the term (e.g., year 29), the majority goes toward the principal. This calculator estimates your total interest paid to help you see the "true cost" of the home beyond just the purchase price.
Use this calculator to experiment with different home prices, down payments, and interest rates to find a mortgage plan that fits your budget comfortably.