There is such a table:

<table class="mable"> <tr class="t"><td>4</td></tr> <tr class="t"><td>3</td></tr> <tr class="t"><td>2</td></tr> <tr class="t"><td>1</td></tr> </table> 

and through js when loading I add line numbers to each line in descending order:

 tr=$('.mable tr.t').length; //alert(tr); //tr=0; $('.t').each(function(){ $(this).append('<div class="tr-number">'+tr+' эт.</div>'); tr--; }); 

The code works as it should.

But if this table is one. If there are several tables , it shows incorrectly. In short, I don’t know how to access a table row through $(this) inside each();

    2 answers 2

    See an example in action

    HTML

     <table class="table"> <tr class="t"><td>4</td></tr> <tr class="t"><td>3</td></tr> <tr class="t"><td>2</td></tr> <tr class="t"><td>1</td></tr> </table> <table class="table"> <tr class="t"><td>5</td></tr> <tr class="t"><td>5</td></tr> <tr class="t"><td>4</td></tr> <tr class="t"><td>3</td></tr> <tr class="t"><td>2</td></tr> <tr class="t"><td>1</td></tr> </table> 

    Js

     $('.table').each(function(){ var row = $('.t', this); var numRows = row.length; row.each(function(){ $(this).find('td').append('<div class="tr-number">' + numRows + ' эт.</div>'); numRows--; }); }); 
    • Thanks @Deonis, but without find it turned out: $ (this) .append ('<div class = "tr-number">' + numRows + 'fl. </ Div>'); and row with 0 start? - Vfvtnjd
    • I can give you a link to an example - Deonis
    • Sorry, I have a mistake, corrected :) - Vfvtnjd

    try to cycle through the tables themselves, and already in the loop find the necessary cells

     $('table').each(function(){ var tr = $(this).find('tr'); }); 
    • thanks, but the answer @Deonis -a completely satisfies me)) - Vfvtnjd
    • four
      no big deal , i'm not worried - makregistr