I am trying to dynamically add a row to a table on the html upon a click event with JQuery 3.3.1 and Bootstrap 4.
HTML:
<table id='tblAddedCallsign' class='table table-striped'>
</table>
JAVASCRIPT:
var row = '<tr>' +
'<td class="text-right">' + callsign_id + '</td>' +
'<td class="text-right">' + callsign_name + '</td>' +
'<td class="text-right"><input type="button" class="btn btn-primary delete" value="Delete" data-id="' + callsign_id + '"></td>' +
'</tr>';
$('#tblAddedCallsign').append(row);
When the click event occurs, html is rendered like:
<table id="tblAddedCallsign" class="table table-striped">
<tr>
<td class="text-right">360</td>
<td class="text-right">BIRDDOG 386</td>
<td class="text-right"><input type="button" class="btn btn-primary delete" value="Delete" data-id="360"></td>
</tr>
<tr>
<td class="text-right">607</td>
<td class="text-right">BIRDDOG 376</td>
<td class="text-right"><input type="button" class="btn btn-primary delete" value="Delete" data-id="607"></td>
</tr>
</table>
I was actually expecting it to be:
<table id="tblAddedCallsign" class="table table-striped">
<tbody>
<tr>
<td class="text-right">360</td>
<td class="text-right">BIRDDOG 386</td>
<td class="text-right"><input type="button" class="btn btn-primary delete" value="Delete" data-id="360"></td>
</tr>
<tr>
<td class="text-right">607</td>
<td class="text-right">BIRDDOG 376</td>
<td class="text-right"><input type="button" class="btn btn-primary delete" value="Delete" data-id="607"></td>
</tr>
</tbody>
</table>
So the problem is tbody was not automatically added into the table element upon the click event, so that the Bootstrap table style was not applied.
Then you need to put
<tbody>in htmlThere is two way
<tbody>with dynamic html which you append<tbody>to your table and append row in that body