title: How to find a parent with a known class in jQuery?
body: <p>I have a <code>&lt;div&gt;</code> that has many other <code>&lt;div&gt;</code>s within it, each at a different nesting level. Rather than give every child <code>&lt;div&gt;</code> an identifier, I rather just give the root <code>&lt;div&gt;</code> the identifier. Here’s an example:</p>
  
  <pre><code>&lt;div class="a" id="a5"&gt;
    &lt;div class="b"&gt;
      &lt;div class="c"&gt;
        &lt;a class="d"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>If I write a function in jQuery to respond to class <code>d</code> and I want to find the ID for its parent, class <code>a</code>, how would I do this? </p>
  
  <p>I cannot simply do <code>$('.a').attr('id');</code>, because there are multiple class <code>a</code>s. I could find its parent’s parent’s parent’s ID but that seems of poor design, slow, and not very polymorphic (I would have to write different code for finding the ID for class <code>c</code>).</p>