$("#tableid tbody:last").append(html);
This creates table rows dynamically. Each newly created row has a "remove" button.
Now if i click that remove button that row will be deleted. How can i do this.
Thanks in advance.
The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements. Parameters: It accepts single parameter selector which is optional.
The deleteRow() method removes the row at the specified index from a table. Tip: Use the insertRow() to create and insert a new row.
$(buttonSelector).live ('click', function ()
{
$(this).closest ('tr').remove ();
}
);
using .live
to bind your event will bind it automatically when your row is dynamically added.
Edit
live
is now deprecated, since version 1.7 I think.
The way to go now is using on
instead of live
.
$('#tableid').on('click', buttonSelector, function(){
$(this).closest ('tr').remove ();
});
See the doc.
You can use this code to delete the parent row containing the clicked button:
$(myButtonSelector).click(function(){
$(this).parents('tr').first().remove();
});
For a live example see this link.
For more information see this article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With