I need the page to run like this: The HTML loads, and the first prompt asks user for input. Prompt is submitted, and the running total is changed and the page displays it. Second prompt is asked, submitted, and then running total is changed and displayed again. Same thing for the final prompt. I believe everything is working completely fine except I can’t find a way to make the prompts show up after all of the HTML is loaded, and for the total to update and be displayed after each prompt. I’ve tried window.onload, setTimeout, DOMContentLoader, etc. but none of them seem to work.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reno Calculator</title>
<script src="homerenovation.js" defer></script>
</head>
<body>
<h1>Reno calculator <img src="2chicks.jpg"></h1>
<p class="paint" style="background-color:grey;"></p>
<p class="furniture" style="background-color:pink;"></p>
<p class="floorCoverings"></p>
<h3 class="totalCost"></h3>
</body>
</html>
Javascript:
function addPaintAndSupplies(totalCost, callback) {
let cost = prompt("Enter the cost for the paint and supplies :");
cost = parseFloat(cost);
if (cost > 100){
cost *= 1.1;
}
// Get a handle for the paint paragraph
let paintArea = document.querySelector(".paint");
paintArea.innerHTML = `Paint $ ${cost.toFixed(2)}`;
callback(totalCost + cost);
return (totalCost + cost);
}
const addFloorCoverings = function(totalCost, callback) {
let cost = prompt("Enter the cost for the floor coverings :");
cost = parseFloat(cost);
if (cost < 500){
cost *= .85;
}
// Get a handle for the floor coverings paragraph
let floorCoveringsArea = document.querySelector(".floorCoverings");
floorCoveringsArea.innerHTML = `Floor Coverings $ ${cost.toFixed(2)}`;
callback(totalCost + cost);
return (totalCost + cost);
}
const addFurniture = (totalCost, callback) => {
let cost = prompt("Enter the cost for the furniture :");
cost = parseFloat(cost);
if (cost < 500){
cost *= .90;
}
// Get a handle for the furniture paragraph
let furnitureArea = document.querySelector(".furniture");
furnitureArea.innerHTML = `Furniture $ ${cost.toFixed(2)}`;
callback(totalCost + cost);
return (totalCost + cost);
}
const updateTotals = (cost) => {
// Get a handle for the total cost header
let totalsArea = document.querySelector(".totalCost");
totalsArea.innerHTML = `Total $ ${cost.toFixed(2)}`;
}
let totalCost = 0;
totalCost = addPaintAndSupplies(totalCost, updateTotals);
totalCost = addFloorCoverings(totalCost, updateTotals);
totalCost = addFurniture(totalCost, updateTotals);
2
Answers
You can use Window’s
load
event to wait for the document to render before prompting the user for input, and you can usesetTimeout
with no delay to allow the DOM to update between inputs. WithoutsetTimeout
, all three inputs are processed in the same event loop, and the DOM is unable to update in between.Here’s a runnable example implemented this way
Could you try this
As a good practice you should move the
script
tag to the line before the end of body tag</body>
where-should-i-put-script-tags-in-html-markup