JQuery add row to fourth to last row

I have a table of accounts. The last four lines are the following, starting with the last: Grand Total, Tax, Subtotal, Add a direct link.

So I need to add a line before "Add link line".

This thread Add Table Row in jQuery shows how to add a row after the last row. I just need to change it to add a line to the fourth to the last line.

+6
source share
4 answers

how about adding a class to your shared string

<tr class="grand-total"></tr> 

then in jquery you do

 $('#myTable tr.grand-total').before('<tr></tr>'); 

this way you are not doing this based on a position that can change, but instead is based on something meaningful like a β€œtotal amount"

+19
source

You want a negative .eq :

 $("#table tr").eq(-4).before( $("<tr>").append( $("<td>") // ... ) ); 
+10
source

Use . before () instead . after () :

 $('#myTable tr:last').before('<tr>...</tr><tr>...</tr>'); 
+2
source

You can go to the last line and then go with prev ()

 $(function(){ $("#myTable tr:last") .prev().prev().prev().prev() .after("<tr><td>x</td></tr>"); }); 
+2
source

Source: https://habr.com/ru/post/903855/


All Articles