1
1
const productTable = document . getElementById ( "product-table" ) ;
2
2
const productRows = productTable . children ;
3
3
4
+ const btnAddProduct = document . getElementById ( "add-product" ) ;
4
5
const tipInput = document . getElementById ( "tip" ) ;
5
6
6
7
const btnCalculate = document . getElementById ( "calculate" ) ;
@@ -10,6 +11,35 @@ const subtotalSpan = document.getElementById("subtotal");
10
11
const finalTipSpan = document . getElementById ( "final-tip" ) ;
11
12
const totalSpan = document . getElementById ( "total" ) ;
12
13
14
+ let totalRows = 0 ;
15
+
16
+ function addProduct ( ) {
17
+ totalRows ++ ;
18
+ const currentRow = totalRows ;
19
+ const htmlString = `
20
+ <td>
21
+ <input type="text" />
22
+ </td>
23
+ <td>
24
+ <input type="number" />
25
+ </td>
26
+ <td>
27
+ <input type="number" />
28
+ </td>
29
+ <td>
30
+ <span></span>
31
+ </td>
32
+ ` ;
33
+
34
+ const row = document . createElement ( "tr" ) ;
35
+ row . innerHTML = htmlString ;
36
+ const priceInput = row . children [ 1 ] . firstElementChild ;
37
+ const unitInput = row . children [ 2 ] . firstElementChild ;
38
+ priceInput . addEventListener ( "focusout" , ( ) => calculateRow ( currentRow ) ) ;
39
+ unitInput . addEventListener ( "focusout" , ( ) => calculateRow ( currentRow ) ) ;
40
+ productTable . appendChild ( row ) ;
41
+ }
42
+
13
43
function calculateRow ( row ) {
14
44
const priceValue = productRows [ row ] . children [ 1 ] . firstElementChild . value ;
15
45
const unitValue = productRows [ row ] . children [ 2 ] . firstElementChild . value ;
@@ -66,5 +96,6 @@ for (let row = 0; row < productRows.length; row++) {
66
96
unitInput . addEventListener ( "focusout" , ( ) => calculateRow ( row ) ) ;
67
97
}
68
98
99
+ btnAddProduct . addEventListener ( "click" , addProduct ) ;
69
100
btnCalculate . addEventListener ( "click" , calculateTotal ) ;
70
101
btnClean . addEventListener ( "click" , cleanTotal ) ;
0 commit comments