views:

216

answers:

2

Wordpress wraps images with captions in a div with a class of .wp-caption. I'm looking for a way to select images that don't have this div so I can wrap them in different div. (to keep a consistent border around all the images)

<div class="blog-post-content"> 
 <div id="attachment_220" class="wp-caption alignleft" style="width: 310px">
  <a href="/somewhere/"><img class="size-medium wp-image-220" src="/path/to/image" alt="" width="300" height="280" /></a>
  <p class="wp-caption-text">Caption Text</p>
 </div>
 <p>This is the body of the post</p>
</div>

To test my selector, I'm just trying to add a green border. I can handle the .wrap() once the selector is working.

The most promising of my attempts is:

$('.blog-post-content img').parent('div:not(".wp-caption")').css('border', '2px solid green');

... but no luck.

+2  A: 

How about this: (untested)

$('.blog-post-content img').filter(function(){
    return !$(this).parents('div').hasClass('wp-caption');
}).css('border', '2px solid green');
John McCollum
You'll need parents() instead of parent() because there seems to be an extra element (an a) between the img and the div. Otherwise seems fine. Edit: Infact you could just use return !$(this).parents('div.wp-caption').length;, but that's just nitpicking.
Matti Virkkunen
Thank you, edited appropriately. :)
John McCollum
.length means one fewer function call, but that may be prematurely optimising. I'll leave it for OP to decide. Thanks for your input.
John McCollum
This works great whether or not the image is used in a link. I was NOWHERE near this when I was working though it. Thanks a lot. Stackoverflow rocks.
Alex
A: 

try:

$('.blog-post-content img').parent(':not("div.wp-caption")')

Not if what Matti says abotu the a element in the hierarchy then the above wont work.

prodigitalson