Skip to content

Commit e0684ee

Browse files
authored
Update index.php
- Using "let" instead of "var" to declare the "counter" variable. This will make the variable block-scoped instead of function-scoped. - Using template literals instead of concatenation to improve the readability of the code. - Using more descriptive variable and function names to make the code easier to understand. - Using jQuery to remove the selected row instead of accessing the DOM directly. - the `add-row-btn` click handler now adds a "Remove" button to each new row - improved the counter to correctly update when adding/removing rows
1 parent 8f213e8 commit e0684ee

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

index.php

+49-20
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,56 @@
121121

122122
<script type="text/javascript">
123123

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() {
138125

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);
139159
});
140-
});
141160

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+
});
147175
</script>
176+

0 commit comments

Comments
 (0)