i have a simple html table where i want to delete and add rows dynamically. the html table has a delete icon, by clicking on it a jquery script deletes the row.
code for the table:
<table id="table1"><tr><td>
<img class="delete" alt="delete" src="delete_icon.png" />
</td></tr></table>
a link adds a new row:
<a href="#" name="addRow">Add Row</a>
both jquery scripts above the html table code:
<script type="text/javascript">
/* delete row */
$(document).ready(function () {
$('#table1 td img.delete').click(function () {
$(this).parent().parent().remove();
});
});
/* add new row */
$(document).ready(function () {
// Code between here will only run when the document is ready
$("a[name=addRow]").click(function () {
// Code between here will only run when the a link is clicked and has a name of addRow
$("table#table1 tr:last").after('<tr><td><img class="delete" alt="delete" src="@Url.Content("~/content/delete_icon.png")" /></td></tr>');
return false;
});
});
</script>
the problem is the following: both delete and insert operations work. however when i add a new row and try to delete this row, nothing happens. i can only delete already existing rows, the jquery script doesn't work on newly added rows.
any ideas?