I have this example table
<table border="1" id="tabla">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
I try to add content dynamically with Jquery with this
$("#tabla").find('tfoot').append($('<td><b>Total</b></td><td>a</td><td>b</td>'));
But, don't works
If a use firebug to inspect table, this have a tfoot but empty. ¿How to add dynamically content to a tfoot without added content previously?
The createTFoot() method creates an empty <tfoot> element and adds it to the table. Note: If a <tfoot> element already exists on the table, the createTFoot() method returns the existing one, and does not create a new one. Note: The <tfoot> element must have one or more <tr> tags inside.
The <tfoot> tag is used to group footer content in an HTML table. The <tfoot> element is used in conjunction with the <thead> and <tbody> elements to specify each part of a table (footer, header, body). Browsers can use these elements to enable scrolling of the table body independently of the header and footer.
To add a row, define a variable that keeps the count of the total number of that now exists in the table. Then we will use the jQuery “click” event to detect a click on the add row button and then use the . append() method of jQuery to add a row in the table.
a solution without changing your html would be to add the tfoot first:
$(function($){
var foot = $("#tabla").find('tfoot');
if (!foot.length) foot = $('<tfoot>').appendTo("#tabla");
foot.append($('<td><b>Total</b></td><td>a</td><td>b</td>'));
})
Try adding an explicit tfoot
tag on your HTML
<table border="1" id="tabla">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
<tfoot></tfoot>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With