Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery- Get all hyperlinked images

I am trying to do something with each hyperlinked image within a div with class of sampleclass:

<div class="sampleclass">
  <a href="#">text hyperlink</a> <!-- don't touch this -->
  <a href="#"><img src="image.jpg"></a> <!-- only touch this -->
</div>

Here is what I have:

$('.sampleclass a > img').(function() {
    $(this).addClass("someotherclass");
});

This doesn't seem to work. Any suggestions?

like image 977
MultiDev Avatar asked Dec 05 '25 13:12

MultiDev


1 Answers

Something is missing here.

$('.sampleclass a > img').(function() {
    $(this).addClass("someotherclass");
});

This:

$('.sampleclass a > img').each(function() {
    $(this).addClass("someotherclass");
});

Note that if you're only doing simple things like that, you can also omit the call to each .each().

$('.sampleclass a > img').addClass("someotherclass");
like image 63
MildlySerious Avatar answered Dec 07 '25 02:12

MildlySerious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!