24120

How to delete a row from a dynamic generate table using jquery?

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(); });

Answer1:

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(); });

Answer2:

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(); });

Recommend

  • ASYNC in Node.JS
  • Vaadin Table does not update no matter what I try
  • xlwings function to find the last row with data
  • using row range to dim array, and use array to iterate through For loop
  • Create column of type double precision[] with liquibase
  • How to select full range in excel vba
  • Submitting two different forms with an external Submit button not working properly
  • Yii - Error on adding foreign Keys using migrate
  • Rails: Correct Way of Using the method of PostController in the Index file
  • vba paste values and keep source formatting?
  • what can i do if for loop repeat single row from mysql database?
  • Running scripts from Perl CGI programs with root permissions
  • Selecting multiple elements with Selenium
  • How to create an HtmlHelper extension method that will bind an IEnumerable to a table
  • Is 'if element in aList' possible with Django templates?
  • Get the Id/name of an element from dynamically generated HTML JQuery
  • Selenium - How to skip the record from the Set after the its first occurrence
  • How to open html table in xls on click of a button
  • how to get username into sql trigger when multiple users signed on from asp membership
  • wxPython: displaying multiple widgets in same frame
  • Custom validator control occupying space even though display set to dynamic
  • req.body is undefined - nodejs
  • sending/ receiving email in Java
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • jquery mobile loadPage not working
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • How to stop GridView from loading again when I press back button?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • Binding checkboxes to object values in AngularJs
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?