jQuery - How to determine if a parent element exists?

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-04-22T14:52:36Z Indexed on 2010/04/22 14:53 UTC
Read the original article Hit count: 138

Filed under:
|

Hi,

I'm trying to dynamically and a link to an image, however I cannot correctly determine is the parent link already exists.

This is what I have,

if (element.parent('a'.length) > 0)
{   
      element.parent('a').attr('href', link);            
}
else
{   
      element.wrap('<a></a>');
      element.parent('a').attr('href', link);     
}

Where element refers to my img element and link refers to the url to use.

Every time the code runs, the else clause is performed, regardless of whether or not the img tag is wrapped in an a tag.

Can anyone see what I'm doing wrong?

Any advice appreciated.

Thanks.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about parent