Search code examples
jqueryhtmlclone

Clone table row and increment IDs by 1


In my js code below I am cloning the last row of my table and attempting to increment the IDs by one.

function addNewRow() {

$('#FinancialDataTable tbody').append($('#FinancialDataTable tbody tr:last').clone());

$('#FinancialDataTable tr').each(function(i) {
$(this).find('tr:last');
var selectinput = $(this).find('select');
var textinput = $(this).find('input');
var textarea = $(this).find('textarea');
i++;
selectinput.eq(0).attr('id', 'FinancialItem'+i);
textinput.eq(0).attr('id', 'FinancialAmount'+i);
textarea.eq(0).attr('id', 'FinancialDescription'+i);
});

}

I have the following code for my table rows in the table:

<table id="FinancialDataTable">
<tr>
  <td>
    <div class="form-group">
        <select class="form-control" id="FinancialItem"></select>
    </div>
</td>
  <td>
    <div class="form-group">
        <input class="form-control" id="FinancialAmount"></select>
    </div>
  </td>
  <td>
    <div class="form-group">
        <textarea class="form-control" id="FinancialAmount"></select>
    </div>      
</td>
</tr>
</table>
    <button type="button">Add New Row</button>

Instead of incrementing the newly added row by one it changes the original row to:

    <table id="FinancialDataTable">
<tr>
  <td>
    <div class="form-group">
        <select class="form-control" id="FinancialItem2"></select>
    </div>
</td>
  <td>
    <div class="form-group">
        <input class="form-control" id="FinancialAmount2"></select>
    </div>
  </td>
  <td>
    <div class="form-group">
        <textarea class="form-control" id="FinancialAmount2"></select>
    </div>      
</td>
</tr>
</table>
    <button type="button">Add New Row</button>

and when I click the button again I get:

    <table id="FinancialDataTable">
<tr>
  <td>
    <div class="form-group">
        <select class="form-control" id="FinancialItem3"></select>
    </div>
</td>
  <td>
    <div class="form-group">
        <input class="form-control" id="FinancialAmount3"></select>
    </div>
  </td>
  <td>
    <div class="form-group">
        <textarea class="form-control" id="FinancialAmount3"></select>
    </div>      
</td>
</tr>
</table>
    <button type="button">Add New Row</button>

Any help would be appreciated! JSFIDDLE


Solution

    1. Remove the unecesarry $(this).find('tr:last');, you don't need it since you are already using $('#FinancialDataTable tr').each() to loop through the table rows.
    2. Remove the i++;, as .each() does the incrementing for you.
    3. If you want the ID of the first row to stay #FinancialDataTable and not become #FinancialDataTable1, just add a return in case i is one, which means you are currently looking at the first row.

    Your final code would look like this: (JSFiddle)

    $('.btn').click(function () {
    
        $('#FinancialDataTable tbody').append($('#FinancialDataTable tbody tr:last').clone());
    
        $('#FinancialDataTable tr').each(function(i) {
            if (i === 1)
                return;
    
            var selectinput = $(this).find('select');
            var textinput = $(this).find('input');
            var textarea = $(this).find('textarea');
            selectinput.eq(0).attr('id', 'FinancialItem' + i);
            textinput.eq(0).attr('id', 'FinancialAmount' + i);
            textarea.eq(0).attr('id', 'FinancialDescription' + i);
        });
    
    });