What is the best method in jQuery to add an additional row to a table as the last row?
Is this acceptable?
$('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
Are there limitations to what you can add to a table like this (such as inputs, selects, number of rows)?
I recommend
as opposed to
The
first
andlast
keywords work on the first or last tag to be started, not closed. Therefore, this plays nicer with nested tables, if you don't want the nested table to be changed, but instead add to the overall table. At least, this is what I found.This can be done easily using the "last()" function of jQuery.
You can use this great jQuery add table row function. It works great with tables that have
<tbody>
and that don't. Also it takes into the consideration the colspan of your last table row.Here is an example usage:
I Guess i have done in my project , here it is:
html
js
You can cache the footer variable and reduce access to DOM (Note: may be it will be better to use a fake row instead of footer).
Neil's answer is by far the best one. However things get messy really fast. My suggestion would be to use variables to store elements and append them to the DOM hierarchy.
HTML
JAVASCRIPT