I am trying to select the anchor tag and add a target attribute. When it is wrapped in a image with the class size-thumbnail. Anyone know why this wont work?
<a href="example"><img class="size-thumbnail" src="example"></a>
jquery
$('.size-thumbnail:parent').attr('target','_blank');
Try this:
$('a').has('img.size-thumbnail').attr('target','_blank');
or
$('a.has(img.size-thumbnail)').attr('target','_blank');
You have the meaning of :parent
backwards — it selects elements which are parents, not the parent of the selected element. Try this instead:
$('.size-thumbnail').parent().attr('target','_blank');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With