Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select parent a of an image

Tags:

jquery

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');
like image 705
ThomasReggi Avatar asked Dec 22 '22 19:12

ThomasReggi


2 Answers

Try this:

$('a').has('img.size-thumbnail').attr('target','_blank'); 

or

$('a.has(img.size-thumbnail)').attr('target','_blank'); 
like image 150
Chandu Avatar answered Jan 05 '23 05:01

Chandu


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');
like image 45
Ben Blank Avatar answered Jan 05 '23 06:01

Ben Blank