Reputation: 1576
I've this code
$('#parent').each(function(){
$('#parent p a:not(:has(img))').addClass('happy_cats');
});
<div id="parent">
<p><a href="#"><img src="http://cdn.playbuzz.com/cdn/0079c830-3406-4c05-a5c1-bc43e8f01479/7dd84d70-768b-492b-88f7-a6c70f2db2e9.jpg" width="100" height="100"></a></p>
<p><a href="#">lol</a><p>
</div>
<div id="parent">
<p>
<a href="#"><img src="http://cdn.playbuzz.com/cdn/0079c830-3406-4c05-a5c1-bc43e8f01479/7dd84d70-768b-492b-88f7-a6c70f2db2e9.jpg" alt="ddd" width="100" height="100"></a></p>
<p><a href="#">lol</a><p>
</div>
I want to add the class happy_cats
on links which have no image inside.
I try this code, it's only works on the first div #parent
Thanks for the help !
Upvotes: 1
Views: 52
Reputation: 996
you can do
$('[id="parent"]').each(function(){
$(this).find('p a:not(:has(img))').addClass('happy_cats');
});
or probably just
$('[id="parent"] p a:not(:has(img))').addClass('happy_cats');
$('#id')
will just find the first match
Upvotes: 0
Reputation: 7711
Use class
instead of id
$('.parent').each(function(){
$('.parent p a:not(:has(img))').addClass('happy_cats');
});
<div class="parent">
<p><a href="#"><img src="http://cdn.playbuzz.com/cdn/0079c830-3406-4c05-a5c1-bc43e8f01479/7dd84d70-768b-492b-88f7-a6c70f2db2e9.jpg" width="100" height="100"></a></p>
<p><a href="#">lol</a><p>
</div>
<div class="parent">
<p>
<a href="#"><img src="http://cdn.playbuzz.com/cdn/0079c830-3406-4c05-a5c1-bc43e8f01479/7dd84d70-768b-492b-88f7-a6c70f2db2e9.jpg" alt="ddd" width="100" height="100"></a></p>
<p><a href="#">lol</a><p>
</div>
Upvotes: 2