Top html-table Questions

List of Tags
Darryl Hein

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)?

Answered By: Luke Bennett ( 611)

The approach you suggest is not guaranteed to give you the result you're looking for - what if you had a tbody for example:

<table id="myTable">

You would end up with the following:

<table id="myTable">

I would therefore recommend this approach instead:

$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');

You can include anything within the after() method as long as it's valid HTML, including multiple rows as per the example above.

Update: Revisiting this answer following recent activity with this question. eyelidlessness makes a good comment that there will always be a tbody in the DOM; this is true, but only if there is at least one row. If you have no rows, there will be no tbody unless you have specified one yourself.

DaRKoN_ suggests appending to the tbody rather than adding content after the last tr. This gets around the issue of having no rows, but still isn't bulletproof as you could theoretically have multiple tbody elements and the row would get added to each of them.

Weighing everything up, I'm not sure there is a single one-line solution that accounts for every single possible scenario. You will need to make sure the jQuery code tallies with your markup.

I think the safest solution is probably to ensure your table always includes at least one tbody in your markup, even if it has no rows. On this basis, you can use the following which will work however many rows you have (and also account for multiple tbody elements):

$('#myTable > tbody:last').append('<tr>...</tr><tr>...</tr>');

I've been using

word-wrap: break-word

to word wrap text in divs and spans, but it doesn't seem to work in table cells. I have a table set to width:100%, with one row and two columns. Text in columns, although styled with the above word-wrap attribute, doesn't wrap and causes the text to go past the bounds of the cell.

Any suggestions on how to work this? This happens on FF, Chrome and IE.

EDIT: Here's what the source looks like:

<table style="width: 100%;"><tr>
<td align="left"><div style="word-wrap: break-word;"> word</div></td>
<td align="right"><span style="display: inline;">Foo</span></td>

The text "" it larger than the bounds of my page but it doesn't break with the above html. I've tried the suggestions below of adding text-wrap:suppress and text-wrap:normal but neither helped.

Answered By: Marc ( 178)

The following works for me in IE. Note the addition of table-layout:fixed CSS Attribute

<table style="table-layout: fixed; width: 100%">
<tr><td style="word-wrap: break-word">LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongWord</td></tr>