function calculateMortgage() {
// Get inputs
var homePrice = parseFloat(document.getElementById("homePrice").value);
var downPayment = parseFloat(document.getElementById("downPayment").value);
var interestRate = parseFloat(document.getElementById("interestRate").value);
var loanTermYears = parseInt(document.getElementById("loanTerm").value);
var propertyTaxYear = parseFloat(document.getElementById("propertyTax").value);
var homeInsuranceYear = parseFloat(document.getElementById("homeInsurance").value);
var hoaFees = parseFloat(document.getElementById("hoaFees").value);
// Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTermYears)) {
alert("Please enter valid numbers for all fields.");
return;
}
// Defaults for optional fields if empty/NaN
if (isNaN(propertyTaxYear)) propertyTaxYear = 0;
if (isNaN(homeInsuranceYear)) homeInsuranceYear = 0;
if (isNaN(hoaFees)) hoaFees = 0;
// Calculations
var principal = homePrice – downPayment;
// Handle edge case where down payment >= home price
if (principal <= 0) {
alert("Down payment cannot be greater than or equal to Home Price.");
return;
}
var monthlyRate = (interestRate / 100) / 12;
var numberOfPayments = loanTermYears * 12;
// Mortgage Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
var monthlyPrincipalInterest = 0;
if (interestRate === 0) {
monthlyPrincipalInterest = principal / numberOfPayments;
} else {
monthlyPrincipalInterest = principal * ( (monthlyRate * Math.pow(1 + monthlyRate, numberOfPayments)) / (Math.pow(1 + monthlyRate, numberOfPayments) – 1) );
}
// Additional Monthly Costs
var monthlyTax = propertyTaxYear / 12;
var monthlyInsurance = homeInsuranceYear / 12;
var totalMonthly = monthlyPrincipalInterest + monthlyTax + monthlyInsurance + hoaFees;
var totalLoanCost = (monthlyPrincipalInterest * numberOfPayments);
var totalInterest = totalLoanCost – principal;
var grandTotalCost = totalLoanCost + downPayment; // Cost of house + interest
// Formatting currency
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
// Update UI
document.getElementById("piPayment").innerText = formatter.format(monthlyPrincipalInterest);
document.getElementById("taxMonthly").innerText = formatter.format(monthlyTax);
document.getElementById("insMonthly").innerText = formatter.format(monthlyInsurance);
document.getElementById("hoaMonthly").innerText = formatter.format(hoaFees);
document.getElementById("totalMonthlyPayment").innerText = formatter.format(totalMonthly);
document.getElementById("loanAmountResult").innerText = formatter.format(principal);
document.getElementById("totalInterestResult").innerText = formatter.format(totalInterest);
document.getElementById("totalCostResult").innerText = formatter.format(totalLoanCost);
document.getElementById("resultsArea").style.display = "block";
}
Understanding How Your Mortgage Payment is Calculated
Buying a home is one of the largest financial decisions you will make, and understanding your monthly mortgage payment is crucial for long-term financial stability. This Mortgage Payment Calculator helps you break down the specific components of your monthly housing costs, ensuring you aren't caught off guard by "hidden" fees like property taxes or HOA dues.
The 4 Components of a Mortgage Payment (PITI)
Most mortgage payments consist of four main parts, commonly referred to as PITI:
Principal: The money that goes directly toward paying down your loan balance. In the early years of a 30-year mortgage, this amount is small compared to the interest.
Interest: The cost of borrowing money from your lender. Our calculator uses the standard amortization formula to determine exactly how much interest you pay based on your rate and term.
Taxes: Property taxes are collected by your local government to fund schools and services. These are typically estimated annually and divided by 12 for your monthly payment.
Insurance: Homeowners insurance protects your property against damage. Lenders require this to protect their investment.
How Interest Rates Affect Your Buying Power
Even a small change in interest rates can significantly impact your monthly payment. For example, on a $350,000 loan, the difference between a 6.0% and a 7.0% interest rate is roughly $230 per month. Over the life of a 30-year loan, that small percentage difference can amount to over $80,000 in additional interest costs.
Short Term vs. Long Term Loans
While a 30-year fixed-rate mortgage is the most common choice because it offers lower monthly payments, a 15-year term can save you tens of thousands of dollars in interest. Use the dropdown menu in the calculator above to compare how a 15-year term increases your monthly payment but drastically reduces the total cost of the loan.