tags:

views:

189

answers:

3

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?

A: 

ah i figured it out. nth-child(0) should be nth-child(1) if i want to select the first row. Counting starts from 1

John
A: 

Try using:

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

See if that helps....

BradBrening
+5  A: 

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

fudgey
is there also a way to clone the row without cloning the value attribute of each input element?
John
try just copying the HTML: `$('#entries').append( $('#entries tr:nth-child(1)').html(); )`
fudgey