Newbie here, I have a dynamic add rows function. My target is to decrement the row id and name after I deleted it. The problems I am encountering is whenever i'm deleting a row, the id and name keeps incrementing when i add new one. It should reset or decrement. I have provided my codes below and a screenshot for more explanation. Thank you so much.
var rowIndex = 0;
var rowIndexx = 1;
$("#addrow").on('click', function() {
rowIndex++;
rowIndexx++;
var newRow = '<tr><td><input type="text" value="' + rowIndexx + '" /></td>"' +
'<td><input id="lightBand' + rowIndex + '" name="lightBand' + rowIndex + '" type="number" /></td>"' +
'<td><input id="weight' + rowIndex + '" name="weight' + rowIndex + '" type="number" /></td>"' +
'<td><input id="wingBand' + rowIndex + '" name="wingBand' + rowIndex + '" type="number" /></td>"' +
'<td><input type="button" class="removerow" id="removerow' + rowIndex + '" name="removerow' + rowIndex + '" value="Remove"/></td>';
$("#applicanttable > tbody > tr:last").after(newRow);
});
// I'm missing something here, when I delete, the id and name should be decremented by 1.
$(document).on('click', '.removerow', function() {
$(this).parents('tr').remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered" border="1" id="applicanttable">
<thead>
<tr>
</tr>
</thead>
<tbody>
<div class="row">
<tr>
<th>#</th>
<th>LB#</th>
<th>Weight#</th>
<th>Wingband #</th>
<th>Action</th>
</tr>
<tr id="row_0">
<td>
<input id="#" name="#" type="text" value="1" readonly />
</td>
<td class="labelcell">
<input id="lightBand" name="lightBand" type="number" value="" class="auto"/>
</td>
<td class="labelcell">
<input id="weight" name="weight" type="number" />
</td>
<td class="labelcell">
<input id="wingBand" name="wingBand" type="number" />
</td>
<td class="labelcell">
<input type="button" class="removerow" id="removerow0" name="removerow0" value="Remove" >
</td>
</tr>
</div>
</tbody>
</div>
<tfoot>
<tr>
</tr>
<tr>
<button type="button" id="addrow" style="margin-bottom: 1%;">Add Row</button>
</tr>
</tfoot>
</table>
