Simple use full " Mortgage Calculator Tool" Script in HTML
This code creates a mortgage calculator tool that allows the user to input the loan amount, interest rate, and loan term and calculates the monthly mortgage payment based on these values. The result is displayed in a separate section below the form. The code is responsive, so it adjusts to different screen sizes.
Here's how the code works:
- The HTML code starts with a form that contains three input fields for loan amount, interest rate, and loan term, and a submit button.
- The form is styled using CSS to make it look nice and centered on the page. There is also a separate CSS style for the result section that is initially hidden.
- The JavaScript code selects the form and result section elements using document.querySelector() method and defines two functions -
calculateMortgagePayment()
and . - The
calculateMortgagePayment()
function takes in the principal amount, interest rate, and loan term as arguments and calculates the monthly mortgage payment using the standard formula. It returns the calculated payment rounded off to two decimal places. - The function takes in the calculated payment as an argument, creates a new element to display the result, adds the result to the element, and appends the element to the result section.
- An event listener is added to the form that listens for form submission. When the form is submitted, the event listener prevents the default form submission behavior using , gets the values of loan amount, interest rate, and loan term from the form using method, calculates the mortgage payment using the
calculateMortgagePayment()
function, clears any previous results from the result section, and displays the result using the function.
Overall, this code provides a simple and effective way to calculate the monthly mortgage payment based on the loan amount, interest rate, and loan term using JavaScript.