I'm trying to build an app that helps people list their expenses in a specific order. How can I allow them to add rows at specific points?
Starting with a simple HTML table like this:
<table id='exptable'>
<tr>
<td>(date)</td>
<td><input type='text' placeholder='Expense type (rent, groceries, etc.)'/></td>
<td><input type='number' value='.00'/></td>
<td><input type='button' class='addrow' value='Add Row Below'/></td>
<td><input type='button' class='delrow' value='Delete' /></td>
</tr>
<tr>
<td>(date)</td>
<td><input type='text' placeholder='Expense type (rent, groceries, etc.)'/></td>
<td><input type='number' value='.00'/></td>
<td><input type='button' class='addrow' value='Add Row Below'/></td>
<td><input type='button' class='delrow' value='Delete' /></td>
</tr>
</table>
My "delete button" works fine with this JQuery:
$('.delrow').click(function(){
$(this).parent().parent().remove();
});
As for the insert button, They will ultimately be able to reorder the rows, so assigning each row a unique ID seems infeasible. Here's the atrocious code I have now:
$('.addrow').click(function(){
$("<tr><td></td><td><input type='text' placeholder='Inserted Row'/></td><td><input type='number' value='.00' class='dollar ' size='8'/></td><td><input type='button' class='addrow' value='Add Row Below'/></td><td><input type='button' class='delrow' value='Delete' /></td></tr>").insertAfter(this.parentNode.parentNode);
});
This DOES insert the row where I want it. However, neither of the buttons in the new row work. Why not? Is there a smarter way to insert a row wherever you want?