Reputation: 1496
I have this code:
$(".scrapImg").each(function(index, elem) {
var scrapImgCount = $(this).find("img").length;
if(scrapImgCount > 1) {
$(".scrapImgItem").css("width", "50%");
$(".scrapImgItem").css("height", "80px");
}
else {
$(".scrapImgItem").css("width", "100%");
}
});
I'm able to get the number of child elements (.img) of the divs with class 'scrapImg', but my if ... else statements don't really work. It looks like the else statement is not being executed because the if statement returns true. How can I let these if ... else statements be executed on every div that contains a number of img elements?
Upvotes: 2
Views: 995
Reputation: 82241
You have used correct reference to check the length. You should use the same to set the css too. Try this:
$(".scrapImg").each(function(index, elem) {
var scrapImg = $(this).find("img");
if(scrapImg .length > 1) {
$(scrapImg ).css("width", "50%");
$(scrapImg ).css("height", "80px");
}
else {
$(scrapImg ).css("width", "100%");
}});
Upvotes: 3