Here is the code snippet for my table:
Check out my JSfiddle for a live demo.
function insert_Row() {
var xTable = document.getElementById('partsTable');
var tr = document.createElement('tr');
tr.innerHTML = "<td colspan=2><input type='text' name='parts[]' placeholder='part 1' class='form-control' > </td><td><input type='text' name='price[]' placeholder='price e.g 100' class='form-control' ></td>";
xTable.insertBefore(tr, xTable.lastElementChild.previousElementSibling);
}
<table class="table table-bordered table-striped" id="partsTable">
<thead>
<tr class="bg-primary">
<th colspan=2>Services</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan=2><input type='text' name="parts[]" placeholder="part 1" class='form-control'> </td>
<td><input type='text' name="price[]" placeholder="price e.g 100" class='form-control'></td>
</tr>
<tr>
<td colspan=3><button onclick="insert_Row();">+</button></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="col-md-6">   </td>
<td><strong>Total</strong></td>
<td><strong>$1000.00</strong></td>
</tr>
</tfoot>
</table>
I encountered an issue where the new tr
is being added at the end of the tbody
. I'm looking for a solution to insert the new tr
one place before the last tr
. Is there a way to achieve this?