Jquery: using appendTo in the second-last row of a table

I have a DOM part that I would like to insert on my page. Currently, I'm just blindly using:

$(myblob).appendTo(someotherblob); 

How can I do the same, but add myblob to the second or last line that is inside someotherblob. someotherblob in this case is a table, and I want to add the row above the second so that it is the last.

+23
javascript jquery
Oct 29 '09 at 17:02
source share
9 answers
 $('#mytable tr:last').before("<tr><td>new row</td></tr>") 
+67
Oct 29 '09 at 17:08
source share

You can also select a row using the index selector.

 $('#mytable tr').eq(-1).before("<tr><td>new row</td></tr>") 

Here eq is used to select the last line. eq is the zero index, and the negative index begins with the last. so I used -1 in the index.

+2
Apr 11 '13 at 8:38
source share

Please note that with before () elements should already be inserted into the document (you cannot insert an element before another if it is not on the page).

So, first you need to insert someotherblob:

 $('selector to insert someotherblob at') .append(someotherblob) .find('table tr:last') .prev() .before(myblob); 
+1
Oct 29 '09 at 17:17
source share
  • I appreciate the answer received by orin , which was this one .

  • I tried this and found that the required column is literally offset above the enitre table. This is because I do not use the adad tag for the header column. Once again I started digging the answer and came up with my solution.

  • So, like this: in my scenario, it was required to show the total result of all the records included in each column of type number. I could not perform the calculation if all the lines are not filled on the fly.

HTML:

 <tr class="gTotal"> <th colspan="4" class="head">Grand Total</th> <th><?php echo $grandTtlResumeSntCnt; ?></th> <th><?php echo $grandTtlEvalCnt; ?></th> <th><?php echo $grandTtlUniqEvalCnt; ?></th> <th><?php echo $grandTtlResCnt; ?></th> <th><?php echo $grandTtlUniqResCnt; ?></th> </tr> </table> 

Jquery:

 $('.gTotal').insertBefore('table>tbody>tr:nth-child(2)'); 

let me know if I answer your question.

+1
Jul 25 '13 at 14:29
source share
 $('#mytable tr:last').prev().after("<tr><td>new row</td></tr>") 
+1
May 23 '15 at 6:07
source share

I recently posted some code online that will help you deal with table changes:

http://notetodogself.blogspot.com/2009/08/javascript-insert-table-rows-table-body.html

In addition, you did not understand too much, but, hopefully, the link above will cover all bases

0
Oct 29 '09 at 17:08
source share

I think you want to add a row for each row of the table between the second and last row.

in this case try:

 var rows=someotherblob.find("TR"); var nrows=rows.length; rows.slice(1,(nrows-2)).each(function(){ $(this).append(myblob); }) 
0
Oct 29 '09 at 17:16
source share

A little shot in the dark, but I assume that you have a footer or maybe even data entry at the bottom of the table, and you want to add rows to the data before the footnote. In this case, you should rebuild your table as follows:

 <table id="myTable"> <thead> <tr><!-- header row(s) --></tr> </thead> <tfoot> <tr><!-- footer row(s) --></tr> </tfoot> <tbody> <tr><!-- exciting and possibly dynamically inserted data goes here --></tr> </tbody> </table> 

Yes, tbody appears after tfoot . Ward, huh?

Then you can just use this:

 $("#myTable tbody").append(newrow); 
0
Oct 29 '09 at 20:43
source share

The easiest way is:

 $("#TableName tr:last").prev().before("<tr><td>Hello World</td></tr>"); 
0
Jan 27 '10 at 2:24
source share



All Articles