Skip to content
Fix Code Error

Add table row in jQuery

March 13, 2021 by Code Error
Posted By: 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)?

Solution

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>');
Answered By: Luke

Related Articles

  • SQL query return data from multiple tables
  • How to use java.net.URLConnection to fire and handle HTTP…
  • How can I center all my contents in html?
  • What does "Fatal error: Unexpectedly found nil while…
  • "Large data" workflows using pandas
  • How to pass 2D array (matrix) in a function in C?
  • Smart way to truncate long strings
  • Use of Jquery on scroll event
  • How to handle Vue 2 memory usage for large data (~50 000…
  • What is the worst programming language you ever worked with?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to loop through a plain JavaScript object with the objects as members?

Next Post:

Is there a simple way to delete a list element by value?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error