title: jQuery traverse upwards and find
body: <p>I have a table which uses jQuery sortable to drag-drop and sort elements in a table.  </p>
  
  <p>When dropping an element into a "sortable-row" , I want to read the data-id from the previous "section-row" .</p>
  
  <p>I've managed to achieve this for when the row is dropped in the row directly beneath a "section-row", or 2 levels below.</p>
  
  <p>First level down :</p>
  
  <pre><code>($(ui.item[0]).closest('tr').prev('.stop-row').data('id');
  </code></pre>
  
  <p>Second level down :</p>
  
  <pre><code>$(ui.item[0].previousElementSibling).closest('tr').prev('tr.stop-row').data('id');
  </code></pre>
  
  <p>The problem is that a "section" can have any amount of rows added to it, so I need some kind of loop which will check if the previous element is a "section-row", if not, check on the following previous.</p>
  
  <p>How do i achieve this with jQuery?</p>
  
  <p>My Table:</p>
  
  <pre><code>&lt;table class="table table-bordered table-responsive"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;Assignment&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Type&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Time&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Level&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
  
      &lt;tbody class="sortable"&gt;
          &lt;tr class="section-row" data-id="{{ id }}"&gt;
              &lt;td&gt; Section-{{ id }}&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr class="sortable_row"&gt;
              &lt;td&gt;{{ assignment }}&lt;/td&gt;
              &lt;td&gt;{{ type }}&lt;/td&gt;
              &lt;td&gt;{{ time }}&lt;/td&gt;
              &lt;td&gt;{{ level }}&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>