4

Possible Duplicate:
jQuery to loop through elements with the same class

I am trying to loop through my images that have a class.

for(var i=0; i<sizes.length; i++){
    var imageSize=sizes[i];
    $('.image').width(imageSize);
}

I have 10 images with the same class name, however, I want them to have different image size.

The loop will loop 10 times but I am not sure how to make each image has the specific imagesize.

Can anyone help me with it? My brain is almost fry. Thanks so much!

Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • Your question isn't clear at all. what is sizes and how many images to you have? what are you trying to do? – gdoron Jan 15 '13 at 02:55
  • 1
    Remember: `$(selector)` matches *all elements with that selector*. So you have a collection of images which you're trying to set the width of, and jQuery obliges. – Waleed Khan Jan 15 '13 at 02:56

2 Answers2

9

if all of your images have a class of .image, you can simply do the following

$( '.image' ).each( function ( index ) {
    $( this ).css( 'width', sizes[ index ] );
});
whitneyit
  • 1,226
  • 8
  • 17
0

You could do something like this:

$('.image').each(function(i) {
    $(this).width(sizes[i]);
});

But you would have to make sure that the sizes array contains the correct sizes in the same order that the images appear on the page.

beezir
  • 460
  • 3
  • 10