Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - clone nth row of a table?

Tags:

jquery

clone

I'm trying to use jquery to clone a table row everytime someone presses the add-row button. Can anyone tell me what's wrong with my code? I'm using HTML + smarty templating language in my view. Here's what my template file looks like:

    <table>
            <tr>
                    <td>Description</td>
                    <td>Unit</td>
                    <td>Qty</td>
                    <td>Total</td>
                    <td></td>
            </tr>
    <tbody id="entries">
    {foreach from=$arrItem item=i name=inv}
            <tr>
                    <td>
                            <input type="hidden" name="invoice_item_id[]" value="{$i.invoice_item_id}"/>
                            <input type="hidden" name="assignment_id[]" value="{$i.assignment_id}" />
                            <input type="text" name="description[]" value="{$i.description}"/>
                    </td>
                    <td><input type="text" class="unit_cost" name="unit_cost[]" value="{$i.unit_cost}"/></td>
                    <td><input type="text" class="qty" name="qty[]" value="{$i.qty}"/></td>
                    <td><input type="text" class="cost" name="cost[]" value="{$i.cost}"/></td>
                    <td><a href="javascript:void(0);" class="delete-invoice-item">delete</a></td>
            </tr>
    {/foreach}
    </tbody>
    <tfoot>
            <tr><td colspan="5"><input type="button" id="add-row" value="add row" /></td></tr>
    </tfoot>
    </table>

Here's my Jquery Javascript call, which I know gets fired when I put in an alert() statement. So the problem is with me not knowing how jquery works.

$('#add-row').live('click', function() {$('#entries tr:nth-child(0)').clone().appendTo('#entries');});

So what am I doing wrong?

like image 536
John Avatar asked May 02 '10 18:05

John


2 Answers

First off there is no such thing as nth-child(0), nth-child starts with 1

like image 98
Mottie Avatar answered Sep 22 '22 21:09

Mottie


Try using:

$("tr:nth-child(0)", "#entries")

See if that helps....

like image 32
BradBrening Avatar answered Sep 23 '22 21:09

BradBrening