Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>edit</strong>: this solution works for both your original problem, the problem you mention in your first comment, and the problem you detail in the comment after that.</p> <pre><code>$('.myLinks').click(function() { var findMe = ''; $(this).parents().each(function() { var a = $(this).find('.findme').is('.findme'); var b = $(this).find('.myLinks').is('.myLinks'); if (a &amp;&amp; b) { // look for first parent that // contains .findme and .myLinks $(this).find('*').each(function() { var name = $(this).attr('class'); if ( name == 'findme') { findMe = $(this); // set element to last matching // .findme } if ( name == 'myLinks') { return false; // exit from the mess once we find // .myLinks } }); return false; } }); alert(findMe.text() ); // alerts "find this one" }); </code></pre> <p>this works for your example in the OP as well as a modified example as explained in the comments:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td class="findme"&gt;don't find this one&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="findme"&gt;find this one&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;a href="#" class="myLinks"&gt;find the previous .findme&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="findme"&gt;don't find this one&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>as well as this test case which you added:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td class="findme"&gt;don't find this one&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="findme"&gt;don't find this one&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="findme"&gt;find this one&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;a href="#" class="myLinks"&gt;find the previous .findme&lt;/a&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload