user99999 user99999 - 4 years ago 155
HTML Question

Getting element before parent

I have the following dom tree:

<tr class="tomatch" id="123"></tr>
<span class="matching"></span>

I have a
object set to
element, and I have to get
element from it. How can I do this?

won't work, I tried using
, but it doesn't work either. Same thing with

How can I do this? I have to match the first element before tr parent, because this structure is repeatable.

Answer Source

.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() {
<script src=""></script>
  <tr class="tomatch" id="123"></tr>
        <span class="matching">Click here</span>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download