0

Good evening, I am trying to create a simple app to calculate insurance rates. The 1st input is for the value of the item (whole numbers), the 2nd is for the insurance rates which will be in decimal numbers, once the numbers are entered it will do some additional math to get the charges and then post the total on a document on the web page. I can't seem to get the total to appear on the input field. Any help will be greatly appreciated being that this is my very 1st project.

Thank you, Edwin

total.addEventListener('click',() => {
    const rte1 = parseFloat(document.getElementById('val$').value);
    const rte2 = parseFloat(document.getElementById('valRate').value); 
    const surCharge = rte1 * rte2; 
    const ept = surCharge * .036;
    const totalCharge = surCharge + ept;
        
    document.getElementById('price').innerHTML = totalCharge;
});
<p class="coll mt-4 text-primary">Enter Value:</p>
	<input id="val$" type="text" name="rate1">
	
    <p class="coll mt-4 text-primary">Enter Insurance Rate:</p>
	<input id="valRate" type="text" name="rate2">
	
	<!-- Calculate Button -->
	<button onclick="total" type="button" class="btn btn-primary active d-block mx-auto mt-4">Calculate</button>
	
	<p class="coll mt-5 text-primary">Total charges including IPT:</p>
	<input id="price" type="text">

2 Answers 2

1

Just use onClick in the HTML element to call a Javascript function:

function total() {
    const rte1 = parseFloat(document.getElementById('val$').value);
    const rte2 = parseFloat(document.getElementById('valRate').value); 
    const surCharge = rte1 * rte2; 
    const ept = surCharge * .036;
    const totalCharge = surCharge + ept;

    document.getElementById('price').value = totalCharge;
};
<p class="coll mt-4 text-primary">Enter Value:</p>
	<input id="val$" type="text" name="rate1">
	
    <p class="coll mt-4 text-primary">Enter Insurance Rate:</p>
	<input id="valRate" type="text" name="rate2">
	
	<!-- Calculate Button -->
	<button onclick="total()" type="button" class="btn btn-primary active d-block mx-auto mt-4">Calculate</button>
	
	<p class="coll mt-5 text-primary">Total charges including IPT:</p>
	<input id="price" type="text">

You also need to use .value instead of .innerHTML to set the text in an input.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Dylan! Much appreciated. It worked perfectly. I didn't realize i had to put the () in the onclick="total".
Yes, because I made total a function, so you are calling the total function onclick.
0
document.getElementById('price').innerHTML = totalCharge;

should be

document.getElementById('price').value = totalCharge;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.