Reputation: 12236
I'm trying to get an element by tree traversal but I can't get there. Example HTML:
<tbody>
<tr class="findThis">
<td></td>
<td></td>
</tr>
<tr>
<td class="loopThis"></td>
<td class="loopThis"></td>
</tr>
<tr class="findThis">
<td></td>
<td></td>
</tr>
<tr>
<td class="loopThis"></td>
<td class="loopThis"></td>
</tr>
</tbody>
Example jQuery/javascript:
$('.loopThis').each(function(){
var findThisClass = $(this).parent().parent().children('.findThis');
});
The problem I'm having here is that I this returns all findThis
classes, but I need to have the parent/closest findThis
class for each loopThis
Upvotes: 0
Views: 35
Reputation: 388316
Since you want to find the previous findThis
element of the current loopThis
element, you can find the current tr
element and then its previous tr
sibling
$('.loopThis').each(function(){
var findThisClass = $(this).closest('tr').prev('.findThis');
});
Upvotes: 4