|
121 | 121 |
|
122 | 122 | <script type="text/javascript">
|
123 | 123 |
|
124 |
| -// function to build our form rows every time we click on the "Add row" button |
125 |
| -$(function(){ |
126 |
| - var counter = 1; |
127 |
| - $('p#add_field').click(function(){ |
128 |
| - counter += 1; |
129 |
| - $('#container').append( |
130 |
| - '<tr> \ |
131 |
| - <td>' + counter + '</td> \ |
132 |
| - <td><div class="form-group"><input class="form-control" name="fields['+counter+'][first]" type="text" placeholder="First" required/></div></td> \ |
133 |
| - <td><div class="form-group"><input class="form-control" name="fields['+counter+'][last]" type="text" placeholder="Last" required/></div></td> \ |
134 |
| - <td><div class="form-group"><input class="form-control" name="fields['+counter+'][email]" type="email" placeholder="email" required/></div></td> \ |
135 |
| - <td><input class="btn btn-primary" id="userfiles" name="fields['+counter+'][file_uploaded][]" type="file" required = "required"/></td> \ |
136 |
| - <td><input class="btn btn-danger" type="button" value="Remove" onclick="delRow(this)"></td> \ |
137 |
| - </tr>'); |
| 124 | +$(function() { |
138 | 125 |
|
| 126 | + let rowCounter = 0; |
| 127 | + |
| 128 | + $('#add-row-btn').click(function() { |
| 129 | + rowCounter++; |
| 130 | + |
| 131 | + const newRow = ` |
| 132 | + <tr id="row-${rowCounter}"> |
| 133 | + <td>${rowCounter}</td> |
| 134 | + <td> |
| 135 | + <div class="form-group"> |
| 136 | + <input class="form-control" name="fields[${rowCounter}][first]" type="text" placeholder="First" required> |
| 137 | + </div> |
| 138 | + </td> |
| 139 | + <td> |
| 140 | + <div class="form-group"> |
| 141 | + <input class="form-control" name="fields[${rowCounter}][last]" type="text" placeholder="Last" required> |
| 142 | + </div> |
| 143 | + </td> |
| 144 | + <td> |
| 145 | + <div class="form-group"> |
| 146 | + <input class="form-control" name="fields[${rowCounter}][email]" type="email" placeholder="Email" required> |
| 147 | + </div> |
| 148 | + </td> |
| 149 | + <td> |
| 150 | + <input class="btn btn-primary" name="fields[${rowCounter}][file_uploaded][]" type="file" required> |
| 151 | + </td> |
| 152 | + <td> |
| 153 | + <button class="btn btn-danger" type="button" onclick="removeRow(${rowCounter})">Remove</button> |
| 154 | + </td> |
| 155 | + </tr> |
| 156 | + `; |
| 157 | + |
| 158 | + $('#container').append(newRow); |
139 | 159 | });
|
140 |
| -}); |
141 | 160 |
|
142 |
| -// function to remove selected row |
143 |
| -function delRow(currElement) { |
144 |
| - var parentRowIndex = currElement.parentNode.parentNode.rowIndex; |
145 |
| - document.getElementById("myTable").deleteRow(parentRowIndex); |
146 |
| -} |
| 161 | + function removeRow(rowId) { |
| 162 | + $(`#row-${rowId}`).remove(); |
| 163 | + renumberRows(); |
| 164 | + } |
| 165 | + |
| 166 | + function renumberRows() { |
| 167 | + $('#container tr').each(function(index) { |
| 168 | + const rowNumber = index + 1; |
| 169 | + $(this).find('td:first').text(rowNumber); |
| 170 | + $(this).attr('id', `row-${rowNumber}`); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | +}); |
147 | 175 | </script>
|
| 176 | + |
0 commit comments