JQuery.next () not working

I have a table structure with the following.

<td class="backgroundimage"><img src="02.jpg" border="0" class="foregroundimage"></td>
<td class="backgroundimage"><img src="03.jpg" border="0" class="foregroundimage"></td>

I am trying to get every img src in my table by doing this.

$('.backgroundImage').each(function(index){
    var oldImage = $(this).next("img").attr('src');

    alert(oldImage);
});

This warns undefined. What have I done wrong? Am I using it .next()incorrectly?

+5
source share
2 answers

Yes - .next()looks at the next brother. And not one of your elements tdhas imgsibling.

Perhaps you wanted to use it $(this).find('img')or just $('img', this).

Depending on what you need to do, the following may also be done:

$('.backgroundimage img').each(function() {
    var oldImage = $(this).attr('src');
});
+5
source

Instead:

$(this).next("img")

You should:

$(this).find("img")

Hope this helps

+3
source

All Articles