Add table row to the bottom of a table


/ Published in: jQuery
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.