5

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!

2
  • Your question isn't clear at all. what is sizes and how many images to you have? what are you trying to do? Commented Jan 15, 2013 at 2: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. Commented Jan 15, 2013 at 2:56

2 Answers 2

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 ] );
});
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.