-2

I have some table rows:

<tr class='top'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='total'><td></td><td></td></tr>
<tr class='top'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='total'><td></td><td></td></tr>
...
...
...
<tr class='top'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='data'><td></td><td></td></tr>
<tr class='total'><td></td><td></td></tr>

I have selected a row of class data. Now I would like to find the row of class top that is in the same section and above my selected row.

Is there a jquery command that will do that for me?

Thanks!

user1634700
  • 165
  • 3
  • 12
  • 1
    If you can move each of these sections to a separate tbody it can be done much more easily – Arun P Johny Apr 02 '14 at 23:40
  • Yes, you're right, but I am working with cards I am dealt. You're solution below looks like it is working, I just changed the selector to ('.top') Thanks! – user1634700 Apr 02 '14 at 23:56

1 Answers1

1

You can use a mix of prevAll() and first() like

var $tr == //tr with class data
var $trop = $tr.prevAll('.top').first();
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Hi Arun - thanks for the response! I think this will work, but my selector should be ('.top') instead of ('.data'). Also, the selection that is returned from prevAll will always be the same order? – user1634700 Apr 02 '14 at 23:49