Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://api.jquery.com/parents/" rel="noreferrer"><code>parents</code></a> or <a href="http://api.jquery.com/closest/" rel="noreferrer"><code>closest</code></a> for that, depending on your needs:</p> <pre><code>$("div.test").parents("tr"); // Or $("div.test").closest("tr"); </code></pre> <p>(The initial selector can be anything that matches your <code>div</code>, so <code>".test"</code> would be fine too.)</p> <p><code>parents</code> will look all the way up the tree, possibly matching multiple <code>tr</code> elements if you have a table within a table. <code>closest</code> will stop with the first <code>tr</code> it encounters for each of the <code>div</code>s.</p> <p>Here's an example using <code>closest</code>:</p> <p><a href="http://jsbin.com/usiqir" rel="noreferrer">Live copy</a> | <a href="http://jsbin.com/usiqir/edit" rel="noreferrer">Live source</a></p> <p>HTML:</p> <pre><code>&lt;table&gt; &lt;tr id="first"&gt; &lt;!-- this tr I want to select --&gt; &lt;td&gt; &lt;div class="test"&gt; text &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr id="second"&gt; &lt;!-- this tr I want to select --&gt; &lt;td&gt; &lt;div class="test"&gt; text &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr id="third"&gt; &lt;!-- this tr I want to select --&gt; &lt;td&gt; &lt;div class="test"&gt; text &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>JavaScript:</p> <pre><code>jQuery(function($) { var rows = $("div.test").closest("tr"); display("Matched " + rows.length + " rows:"); rows.each(function() { display("Row '" + this.id + "'"); }); function display(msg) { $("&lt;p&gt;").html(msg).appendTo(document.body); } }); </code></pre> <p>Output:</p> <pre>Matched 3 rows: Row 'first' Row 'second' Row 'third'</pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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