Skip to content

Commit 9f79c44

Browse files
committed
New: Dynamic value caulcuation utils.
1 parent f7fbe9a commit 9f79c44

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

dom/dynamic_value_calculation.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
// Function to calculate total price
3+
function calculateTotal() {
4+
// Get quantity and price values
5+
const quantity = parseFloat(document.getElementById('quantity').value)
6+
const price = parseFloat(document.getElementById('amount').value)
7+
8+
// Calculate total price if inputs are valid
9+
if (!isNaN(quantity) && !isNaN(price)) {
10+
const total = quantity * price
11+
12+
// Display the total price in the DOM
13+
document.getElementById('totalPrice').value = total.toFixed(2)
14+
} else {
15+
// Display 0.00 if inputs are invalid or empty
16+
document.getElementById('totalPrice').value = '0.00'
17+
}
18+
}
19+
// Attach event listeners to input fields
20+
document.getElementById('quantity').addEventListener('input', calculateTotal)
21+
document.getElementById('price').addEventListener('input', calculateTotal)
22+
</script>
23+

0 commit comments

Comments
 (0)