function calculateMortgage() {
// 1. Get Input Values
var homePrice = parseFloat(document.getElementById('mc-home-price').value);
var downPayment = parseFloat(document.getElementById('mc-down-payment').value);
var interestRate = parseFloat(document.getElementById('mc-interest-rate').value);
var loanTermYears = parseInt(document.getElementById('mc-loan-term').value);
var annualTax = parseFloat(document.getElementById('mc-property-tax').value);
var annualIns = parseFloat(document.getElementById('mc-home-insurance').value);
var monthlyHOA = parseFloat(document.getElementById('mc-hoa-fees').value);
// 2. Validate Inputs
if (isNaN(homePrice) || homePrice < 0) homePrice = 0;
if (isNaN(downPayment) || downPayment < 0) downPayment = 0;
if (isNaN(interestRate) || interestRate < 0) interestRate = 0;
if (isNaN(annualTax) || annualTax < 0) annualTax = 0;
if (isNaN(annualIns) || annualIns < 0) annualIns = 0;
if (isNaN(monthlyHOA) || monthlyHOA < 0) monthlyHOA = 0;
// 3. Logic: Principal
var loanAmount = homePrice – downPayment;
if (loanAmount < 0) loanAmount = 0;
// 4. Logic: Monthly P&I Calculation
// Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
var monthlyInterestRate = (interestRate / 100) / 12;
var totalPayments = loanTermYears * 12;
var monthlyPrincipalAndInterest = 0;
if (interestRate === 0) {
monthlyPrincipalAndInterest = loanAmount / totalPayments;
} else {
var x = Math.pow(1 + monthlyInterestRate, totalPayments);
monthlyPrincipalAndInterest = loanAmount * ((monthlyInterestRate * x) / (x – 1));
}
if (isNaN(monthlyPrincipalAndInterest)) monthlyPrincipalAndInterest = 0;
// 5. Logic: Escrow Items
var monthlyTax = annualTax / 12;
var monthlyIns = annualIns / 12;
// 6. Logic: Total
var totalMonthlyPayment = monthlyPrincipalAndInterest + monthlyTax + monthlyIns + monthlyHOA;
// 7. Format Output (Currency)
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
});
// 8. Update DOM
document.getElementById('mc-result-pi').innerText = formatter.format(monthlyPrincipalAndInterest);
document.getElementById('mc-result-tax').innerText = formatter.format(monthlyTax);
document.getElementById('mc-result-ins').innerText = formatter.format(monthlyIns);
document.getElementById('mc-result-hoa').innerText = formatter.format(monthlyHOA);
document.getElementById('mc-result-total').innerText = formatter.format(totalMonthlyPayment);
document.getElementById('mc-result-loan-amount').innerText = formatter.format(loanAmount);
// Show results
document.getElementById('mc-result-container').style.display = 'block';
}
Understanding Your Mortgage Payment
Purchasing a home is likely the largest financial commitment you will make in your lifetime. Understanding exactly where your money goes every month is crucial for financial planning. This Mortgage Calculator is designed to provide a comprehensive breakdown of your monthly housing costs, going beyond just the bank loan to include taxes, insurance, and association fees.
The 4 Pillars of a Mortgage Payment (PITI)
Most lenders refer to your monthly payment as PITI. Here is what that acronym stands for and how each component is calculated in the tool above:
Principal: The portion of your payment that goes directly toward reducing the loan balance ($350,000 Home – $70,000 Down = $280,000 Principal). In the early years of a mortgage, the principal portion is small, but it grows over time.
Interest: The fee charged by the lender for borrowing money. Your interest rate significantly impacts your monthly payment. For example, on a $300,000 loan, a 1% difference in rate can change your payment by over $150 per month.
Taxes: Property taxes are assessed by your local government to fund schools and infrastructure. These are usually estimated annually and divided by 12 to be included in your monthly payment.
Insurance: Homeowners insurance protects your property against damage. Like taxes, the annual premium is typically divided into monthly installments paid into an escrow account.
How the Loan Term Affects Your Wallet
The "Loan Term" is the duration over which you agree to repay the loan. The most common terms are 15 and 30 years.
30-Year Mortgage: This offers lower monthly payments because the repayment is spread out over a longer period. However, you will pay significantly more in total interest over the life of the loan.
15-Year Mortgage: This requires higher monthly payments, but you build equity much faster and usually qualify for a lower interest rate, saving thousands in long-term interest costs.
Example Calculation
Let's assume you are buying a home for $350,000 with a 20% down payment ($70,000). You secure a 30-year fixed-rate mortgage at 6.5% interest.
Loan Amount: $280,000
Principal & Interest: ~$1,770 per month
Property Tax ($4,500/yr): $375 per month
Insurance ($1,200/yr): $100 per month
Total Estimated Payment: ~$2,245 per month
Use the calculator above to adjust these numbers based on your specific loan offer and local tax rates to ensure you stay within your monthly budget.