Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery, Clear / Empty all contents of tbody element?

I thought this would be rather simple but it seems the empty method is not working to clear out a tbody that I have. I would appreciate if anyone knows a proper way to do this, I just want to delete everything contained within the tbody. So far I am trying:

$("#tbodyid").empty(); 

HTML:

<table> <tbody id="tbodyid"> <tr><td>something</td></tr> </tbody> </table> 

NOTE: I am trying to do this to integrate with a plugin written by someone else that I am being made to use for a project. I am generating new <tr><td>new data</td></tr> server-side and want to just be able to wipe out the existing table rows and replace them on AJAX callbacks.

like image 846
Rick Avatar asked Feb 13 '11 06:02

Rick


People also ask

How to clear table tbody in jQuery?

click(function(){ $("#myTable"). find("tr:gt(0)"). remove(); }); });

How to Empty table body in js?

This can be done by using JavaScript. First of all set the ID or unique class to the table. Select the table element and use remove() or detach() method to delete the all table rows.

How do you empty a table in jQuery?

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.

How do I uninstall Tbody?

If you want to remove the tbody tag, you could select the row itself rather than the table, then use the removeChild function.


2 Answers

jQuery:

$("#tbodyid").empty(); 

HTML:

<table>     <tbody id="tbodyid">         <tr>             <td>something</td>         </tr>     </tbody> </table> 

Works for me
http://jsfiddle.net/mbsh3/

like image 64
Marko Avatar answered Sep 17 '22 15:09

Marko


You probably have found this out already, but for someone stuck with this problem:

$("#tableId > tbody").html(""); 
like image 38
Maxi Gis Avatar answered Sep 20 '22 15:09

Maxi Gis