Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add content to a tfoot in a table with Jquery

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?

like image 919
user1783933 Avatar asked May 16 '13 14:05

user1783933


People also ask

How do you add Tfoot to a table?

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.

Is Tfoot a table element?

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.

How do I add a row to a table in button click?

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.


2 Answers

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>'));
})
like image 95
Rene Koch Avatar answered Oct 24 '22 18:10

Rene Koch


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>
like image 25
Claudio Redi Avatar answered Oct 24 '22 18:10

Claudio Redi