Search code examples
javascriptjqueryattributescloneunique

jQuery cloning an element and changing attributes of child elements to keep them unique


I've looked around at a few answers to similar questions but I don't fully understand the answers.

I have a form with a table in it which contains some inputs, as below...

<table class="form right 40 rewardclone_1">
<tr>        
<td><label for="financial_goal_1">Amount</label></td>
<td>Pledge <span class="dollar">&#36;</span><input id="financial_goal_1" class="text dollars" type="text" name="financial_goal_1" /> or more</td>
</tr>
<tr>
    <td><label for="maximum_1">Maximum</label></td>
<td><input id="maximum_1" class="text dollars" type="text" name="maximum_1" /> available (blank for unlimited)</td>
</tr>
<tr>
<td><label for="reward_1">Reward:</label></td>
<td><textarea id="reward_1" name="reward_1"></textarea></td>
</tr>
</table>

I've managed to create a button which clones this entire table when clicked.

$('#add_reward').click(function() {         
  $('.rewardclone').clone().appendTo('#rewards_container');     
});

The problem is I need all those numbers on the attributes (reward_1, maximum_1, etc.) to increment each time the table is cloned.

Any help would be greatly appreciated!

jQuery level: Beginner.

Thanks.


Solution

  • Since it is a small table, I prefer using it as a template instead of cloning and finding matching elements.

    Note: depending on the server technology, you can also load rewardTableTmpl from server for the first time and then use the template for further use.

    DEMO

    $(function() {
        var rewardTableTmpl = '<table class="form right 40 rewardclone">' +
            '<tr><td><label for="financial_goal_{NUM}">Amount</label></td>' +
            '<td>Pledge <span class="dollar">&#36;</span><input id="financial_goal_{NUM}" class="text dollars" type="text" name="financial_goal_{NUM}" /> or more</td>' +
            '</tr><tr>' +
            '<td><label for="maximum_{NUM}">Maximum</label></td>' +
            '<td><input id="maximum_{NUM}" class="text dollars" type="text" name="maximum_{NUM}" /> available (blank for unlimited)</td>' +
            '</tr><tr>' +
            '<td><label for="reward_{NUM}">Reward:</label></td>' +
            '<td><textarea id="reward_{NUM}" name="reward_{NUM}"></textarea></td>' +
             '</tr></table>';
    
        $('#add_reward').click(function() {
            $('#rewards_container').append(rewardTableTmpl.replace(/{NUM}/g, $('rewardclone').length + 1));
        });
    });