File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments