diff --git a/htdocs/index.html b/htdocs/index.html index 3e22717..f1d7739 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -171,9 +171,9 @@ Total: - + Total: - + @@ -325,16 +325,71 @@ const inputs = document.querySelectorAll("#r1, #r2, #r3, #r4, #r5, #r6, #r7, #r8 // Initialize a variable to hold the total let total = 0; -// Loop through each input element and add its value to the total +// Define a function to update the total +function updateTotal() { + // Reset the total to zero + total = 0; + // Loop through each input element and add its value to the total + inputs.forEach(input => { + // Remove the dollar sign from the value and convert it to a number + let value = Number(input.value.replace("$", "")); + // Add the value to the total + total += value; + }); + // Output the updated total to the console + console.log(total); + // Display the updated total in the input box with ID "totalBox1" + document.querySelector('#totalBox1').value = '$' + total.toFixed(2); +} + +// Call the updateTotal function initially to calculate the total based on the initial values of the input fields +updateTotal(); + +// Loop through each input element and add an event listener that calls the updateTotal function whenever its value changes inputs.forEach(input => { - total += Number(input.value); + input.addEventListener("input", updateTotal); +}); + + + \ No newline at end of file