2

My code creates dynamically a table using jquery. I want to add delete functionality to the table. So when clicking on delete image the row should be delete. But deleting works fine just when the table is static. here id my code:

createTable: function () {
    var lastRow = $('#TblInvoiceList tr:last');
    var newRow = $('<tr>');
    newRow.append($('<td>').text($('input.Name').val()), $('<td>').text($('input.GrossAmount').val()));
    newRow.append("<td class='center'><img class='ImgDelete' src='image/ButtonDelete.png' /></td>");
    lastRow.before(newRow);}

and this is the delete function:

$('#TblInvoiceList td img.ImgDelete').click(function () {
    alert("hi");
    $(this).parent().parent().remove();
});

2 Answers 2

2

Use delegate even more efficient than live for dynamic added content do:

$('#TblInvoiceList').delegate('img.ImgDelete', 'click', function(e) {
     alert("hi");
     $(this).parent().parent().remove();
});
Sign up to request clarification or add additional context in comments.

1 Comment

how can i put $(this).parent().parent().remove(); in a method like this: $('#TblInvoiceList').delegate('img.ImgDelete', 'click', function(e) { deleteRow() });
2

try with live as

The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation:

$('#TblInvoiceList td img.ImgDelete').live('click',function () {
    alert("hi");
    $(this).parent().parent().remove();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.