I have the following dom tree:
<tr class="tomatch" id="123"></tr>
<tr>
<td>
<div>
<span class="matching"></span>
</div>
</td>
</tr>
$(this)
.matching
.tomatch
parent()
.prev()
prevUntil('.tomatch')
.closest('.tomatch')
.parents('.tomatch')
.closest
and .parents
are for finding elements that are ancestors of the specified element, but the element you want is a sibling of an ancestor. So you need to do it in two steps -- first go up to the ancestor, then go to the previous element from there.
$(".matching").click(function() {
console.log($(this).closest("tr").prev().attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr class="tomatch" id="123"></tr>
<tr>
<td>
<div>
<span class="matching">Click here</span>
</div>
</td>
</tr>
</table>