loliki
loliki

Reputation: 957

How do I affect only some items in my array Jquery

My Wordpress posts shows images, all images have the .aligncenter class. I would like the images which are less than 400px to have the display:inline-block; style and to reduce their size to 40%.

I tried it this way:

var size = jQuery(".aligncenter").width();
if(jQuery( size < 400)) {
jQuery(".aligncenter").css({
 'display' : 'inline-block',
 'width' : '40%'
});
}

This script affects all images in the post, not only the ones that are less than 400px. How can I affect only images that are less than 400px?

Upvotes: 0

Views: 47

Answers (1)

user7149518
user7149518

Reputation:

$(".algincenter").each(function(){ if($(this).width()<400){$(this).css(...) } })

The code above, takes each image and checks it one by one, think of it as a loop which will run through all the images in your container. If the loop finds an image which is bigger < than 400px it will apply the css styles to that image. If not, it will ignore it.

Upvotes: 1

Related Questions