panthro
panthro

Reputation: 24061

Loop through a list and get an elements image id?

$('#newsImageGallery li').each(function() { 
    alert($(this));
});

In each of my li I have an image with an id, how can I alert that images id?

Upvotes: 0

Views: 240

Answers (3)

hrwath
hrwath

Reputation: 125

Try this:

$('#newsImageGallery li').each(function() { 
    alert($(this).find('img').attr('id'));
});

Upvotes: 1

Sean O
Sean O

Reputation: 2619

alert( $(this).find('img').attr('id') );

Upvotes: 1

h2ooooooo
h2ooooooo

Reputation: 39532

$('#newsImageGallery li').each(function() { 
    alert($(this).find("img").attr("id"));
});

or of course, without the find:

$('#newsImageGallery li img').each(function() { 
    alert($(this).attr("id"));
});

And as Pointy mentions underneath, if you use jQuery 1.6+ you're better off using .prop instead of .attr:

$('#newsImageGallery li img').each(function() { 
    alert($(this).prop("id"));
});

Upvotes: 3

Related Questions