javascript - Add table row in jQuery

ID : 278

viewed : 320

Tags : javascriptjqueryhtml-tablejavascript

Top 5 Answer for javascript - Add table row in jQuery

vote vote


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>'); 
vote vote


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.

vote vote


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>'); 
vote vote


What if you had a <tbody> and a <tfoot>?

Such as:

<table>     <tbody>         <tr><td>Foo</td></tr>     </tbody>     <tfoot>         <tr><td>footer information</td></tr>     </tfoot> </table> 

Then it would insert your new row in the footer - not to the body.

Hence the best solution is to include a <tbody> tag and use .append, rather than .after.

$("#myTable > tbody").append("<tr><td>row content</td></tr>"); 
vote vote


I know you have asked for a jQuery method. I looked a lot and find that we can do it in a better way than using JavaScript directly by the following function.


index is an integer that specifies the position of the row to insert (starts at 0). The value of -1 can also be used; which result in that the new row will be inserted at the last position.

This parameter is required in Firefox and Opera, but it is optional in Internet Explorer, Chrome and Safari.

If this parameter is omitted, insertRow() inserts a new row at the last position in Internet Explorer and at the first position in Chrome and Safari.

It will work for every acceptable structure of HTML table.

The following example will insert a row in last (-1 is used as index):

<html>     <head>         <script type="text/javascript">         function displayResult()         {             document.getElementById("myTable").insertRow(-1).innerHTML = '<td>1</td><td>2</td>';         }         </script>     </head>      <body>                <table id="myTable" border="1">             <tr>                 <td>cell 1</td>                 <td>cell 2</td>             </tr>             <tr>                 <td>cell 3</td>                 <td>cell 4</td>             </tr>         </table>         <br />         <button type="button" onclick="displayResult()">Insert new row</button>                 </body> </html> 

I hope it helps.

Top 3 video Explaining javascript - Add table row in jQuery