MultiDev
MultiDev

Reputation: 10659

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?

Upvotes: 3

Views: 54

Answers (3)

Joren
Joren

Reputation: 3315

This seems to fix it:

 $('.sampleclass a > img').addClass("someotherclass");

Fiddle

Upvotes: 0

Neel
Neel

Reputation: 11741

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

Upvotes: 0

MildlySerious
MildlySerious

Reputation: 9180

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");

Upvotes: 4

Related Questions