I have a bunch of imgs inside a div with a class on them, in my JS I have a for loop that re-sizes the images to fir inside a box without stretching
$('.gallery img')[i].attr('id', 'img' + i);
This is what I tried to get each img to have its own id like 'img1' 'img2' 'img3' etc
but it seems to not work
Replace your for loop with:
$('.gallery img').each(function(i) {
$(this).attr('id', 'img' + i);
// You can also add more code here if you wish to manipulate each IMG element further
});
This block of code loops through each IMG element with a counter i incrementing after every instance, and this counter is suffixed to the id string.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments