Reputation: 12740
I want to add a new TD into cloned table so it will only exist in cloned table not in original one. The reason is, I don't want users to remove original table otherwise there won't be anything to be cloned :)
Currently like this:
<table>
<tr>
<td>
<div class="sizes">size</div>
<div class="sizes">length</div>
</td>
</tr>
</table>
After cloning it should be like this:
<table>
<tr>
<td>
<div class="sizes">size</div>
<div class="sizes">length</div>
</td>
<td class="remove-table"><span>REMOVE</span></td>
</tr>
</table>
JS:
$(document).ready(function () {
$('.add-another-table').click(function (event) {
event.preventDefault();
var clonedTable = $(this).prev('table').clone();
$(this).before(clonedTable);
});
$('div').on('click', '.remove-table', function (event) {
event.preventDefault();
$(this).closest('table').remove();
});
});
Upvotes: 0
Views: 165
Reputation: 67207
Try to .append()
that required element over that clone
,
$(document).ready(function () {
$('.add-another-table').click(function (event) {
event.preventDefault();
var clonedTable = $(this).prev('table').clone();
$(this).before(clonedTable.find('.remove-table').remove().end().find('tr').append('<td class="remove-table"><span>REMOVE</span></td>').end());
});
$('div').on('click', '.remove-table', function (event) {
event.preventDefault();
$(this).closest('table').remove();
});
});
Upvotes: 1