function calculateMortgage() {
// Retrieve elements
var homePriceInput = document.getElementById("mc_home_price");
var downPaymentInput = document.getElementById("mc_down_payment");
var interestRateInput = document.getElementById("mc_interest_rate");
var loanTermInput = document.getElementById("mc_loan_term");
var errorMsg = document.getElementById("mc_error");
var resultsDiv = document.getElementById("mc_results");
// Parse values
var homePrice = parseFloat(homePriceInput.value);
var downPayment = parseFloat(downPaymentInput.value);
var interestRate = parseFloat(interestRateInput.value);
var termYears = parseInt(loanTermInput.value);
// Reset error state
errorMsg.style.display = "none";
resultsDiv.style.display = "none";
// Validation
if (isNaN(homePrice) || isNaN(downPayment) || isNaN(interestRate) || isNaN(termYears)) {
errorMsg.style.display = "block";
return;
}
if (downPayment >= homePrice) {
errorMsg.innerHTML = "Down payment cannot be equal to or greater than home price.";
errorMsg.style.display = "block";
return;
}
// Calculation Logic
var principal = homePrice – downPayment;
var monthlyRate = (interestRate / 100) / 12;
var numberOfPayments = termYears * 12;
var monthlyPayment = 0;
if (interestRate === 0) {
monthlyPayment = principal / numberOfPayments;
} else {
// Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
var x = Math.pow(1 + monthlyRate, numberOfPayments);
monthlyPayment = (principal * x * monthlyRate) / (x – 1);
}
var totalPayment = monthlyPayment * numberOfPayments;
var totalInterest = totalPayment – principal;
// Formatting Helper
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
// Display Results
document.getElementById("mc_monthly_payment").innerHTML = formatter.format(monthlyPayment);
document.getElementById("mc_loan_amount").innerHTML = formatter.format(principal);
document.getElementById("mc_total_interest").innerHTML = formatter.format(totalInterest);
document.getElementById("mc_total_cost").innerHTML = formatter.format(totalPayment);
resultsDiv.style.display = "block";
}
How to Calculate Your Mortgage Payment
Understanding how your monthly mortgage payment is calculated is a crucial step in the home buying process. This calculator helps you estimate your monthly Principal and Interest (P&I) payments based on standard amortization formulas used by lenders.
Factors That Affect Your Monthly Payment
When you take out a mortgage loan, several key variables determine how much you will pay each month and over the life of the loan:
Home Price: The purchase price of the property. This is the starting point for calculating your loan amount.
Down Payment: The amount of money you pay upfront. A larger down payment reduces the principal loan amount, which lowers your monthly payments and the total interest paid. Standard down payments range from 3% to 20%.
Interest Rate: This is the cost of borrowing money, expressed as a percentage. Even a small difference in the interest rate can significantly impact your monthly payment and total loan cost over 30 years.
Loan Term: The length of time you have to repay the loan. The most common terms are 15 and 30 years. Shorter terms generally have higher monthly payments but lower interest rates and less total interest paid.
The Mortgage Amortization Formula
While this calculator handles the math for you instantly, it uses the standard amortization formula:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1 ]
Where:
M = Total monthly payment
P = Principal loan amount (Home Price minus Down Payment)
i = Monthly interest rate (Annual Rate divided by 12)
n = Number of payments (Loan Term in years multiplied by 12)
Principal vs. Interest
In the early years of a mortgage, a large portion of your monthly payment goes toward paying off the interest. As time passes, the portion allocated to interest decreases, and the portion paying down the principal increases. This process is known as amortization.
Additional Costs to Consider
Note that this calculator provides the Principal and Interest (P&I) payment. Your actual monthly housing expense may be higher due to:
Property Taxes: Taxes paid to your local government, often bundled into your monthly mortgage bill via an escrow account.
Homeowners Insurance: Required by lenders to protect the property against damage.
PMI (Private Mortgage Insurance): Usually required if your down payment is less than 20% of the home's value.
HOA Fees: Monthly or annual fees if the property is part of a Homeowners Association.
Use the results above to determine your budget and ensure you are financially prepared for your home purchase.