title: Finding ancestor with specific attribute or class in jQuery
body: <p>I have a DOM element <code>elem</code> that has an <em>ancestor</em> element which has the class <code>myClass</code> (alternatively an attribute with some name). The problem is that <strong>I need to find this ancestor easily with jQuery</strong>. Currently, I am stuck with ugly trial-and-error stuff like </p>
  
  <pre><code>var ancestor = $(elem).parent().parent().parent().parent().parent().parent().parent();
  </code></pre>
  
  <p>If the HTML code changes, the jQuery code easily breaks. </p>
  
  <p>Is it possible to find the ancestor element with more elegant jQuery code?</p>