Categories
html-table javascript jquery

Add table row in jQuery

2601

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

5

  • 3

    Thxs Ash. I too am just learning jQuery and finding it hard to figure out the best way, especially simple things. The reason they are as 2 questions is because I posted one and then almost an hour later I realized I should have put the other one in and didn’t think I should change the first.

    Oct 6, 2008 at 5:53

  • 33

    Because of this: google.com/search?q=jquery+add+table+row

    Oct 5, 2009 at 22:54

  • 19

    FYI – Avoid using multiple appends (slows down performance tremendously), rather build up your string or use JavaScript join which is much faster.

    Apr 16, 2010 at 9:32


  • 1

    see: stackoverflow.com/a/50365764/7186739

    – dev

    May 16, 2018 at 8:24

  • 1

    Just in case if row is too complex, what I do is, keep first row hidden with required structure, make a clone and modify text and insert after first row, this way if you fetch data from ajax response your table will be created, remember clone it outside the loop, then use it to modify content inside loop. $(“#mainTable tbody”).append(row); row is the modified clone copy 🙂

    – Aadam

    May 16, 2018 at 12:00

2273

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">
  <tbody>
    <tr>...</tr>
    <tr>...</tr>
  </tbody>
</table>

You would end up with the following:

<table id="myTable">
  <tbody>
    <tr>...</tr>
    <tr>...</tr>
  </tbody>
  <tr>...</tr>
</table>

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-child').append('<tr>...</tr><tr>...</tr>');

0

    820

    +100

    jQuery has a built-in facility to manipulate DOM elements on the fly.

    You can add anything to your table like this:

    $("#tableID").find('tbody')
        .append($('<tr>')
            .append($('<td>')
                .append($('<img>')
                    .attr('src', 'img.png')
                    .text('Image cell')
                )
            )
        );
    

    The $('<some-tag>') thing in jQuery is a tag object that can have several attr attributes that can be set and get, as well as text, which represents the text between the tag here: <tag>text</tag>.

    This is some pretty weird indenting, but it’s easier for you to see what’s going on in this example.

    0

      333

      So things have changed ever since @Luke Bennett answered this question. Here is an update.

      jQuery since version 1.4(?) automatically detects if the element you are trying to insert (using any of the append(), prepend(), before(), or after() methods) is a <tr> and inserts it into the first <tbody> in your table or wraps it into a new <tbody> if one doesn’t exist.

      So yes your example code is acceptable and will work fine with jQuery 1.4+. 😉

      $('#myTable').append('<tr><td>my data</td><td>more data</td></tr>');
      

      1