Posted By

1man on 03/20/09


Tagged

table DOM jquery row


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

chucktrukk
jfherring
LeeRJohnson
alejandrosuch
jillella


Add table row to the bottom of a table


 / Published in: jQuery
 

URL: http://jquery-howto.blogspot.com/2009/02/add-table-row-using-jquery-and.html

Great little snippit to add a to the bottom of a table. Note the use of the context in the jQuery e.g var n = $('tr:last td', this).length; Will have to use this in the future, very good to know!

  1. /*
  2. Add a new table row to the bottom of the table
  3. */
  4.  
  5. function addTableRow(jQtable){
  6. jQtable.each(function(){
  7. var $table = $(this);
  8. // Number of td's in the last table row
  9. var n = $('tr:last td', this).length;
  10. var tds = '<tr>';
  11. for(var i = 0; i < n; i++){
  12. tds += '<td> </td>';
  13. }
  14. tds += '</tr>';
  15. if($('tbody', this).length > 0){
  16. $('tbody', this).append(tds);
  17. }else {
  18. $(this).append(tds);
  19. }
  20. });
  21. }

Report this snippet  

You need to login to post a comment.