title: jQuery - How to determine if a parent element exists?
body: <p>I'm trying to dynamically and a link to an image, however I cannot correctly determine is the parent link already exists.</p>
  
  <p>This is what I have, </p>
  
  <pre><code>if (element.parent('a'.length) &gt; 0)
  {   
        element.parent('a').attr('href', link);            
  }
  else
  {   
        element.wrap('&lt;a&gt;&lt;/a&gt;');
        element.parent('a').attr('href', link);     
  }
  </code></pre>
  
  <p>Where element refers to my img element and link refers to the url to use.</p>
  
  <p>Every time the code runs, the else clause is performed, regardless of whether or not the img tag is wrapped in an a tag.</p>
  
  <p>Can anyone see what I'm doing wrong?</p>
  
  <p>Any advice appreciated.</p>
  
  <p>Thanks.</p>