BentCoder
BentCoder

Reputation: 12740

Adding a table cell into only cloned table

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();
   });
});

http://jsfiddle.net/b3JdB/

Upvotes: 0

Views: 165

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

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();
    });
});

DEMO

Upvotes: 1

Related Questions