I'm trying to add a class to an image if the height of the image is smaller than the height of the containing div. However, jQuery adds the class to all images if one image is smaller than the containing box. How do I get the jquery to only add the class to the images that are smaller than the containing divs?
I have experience with jQuery whatsoever. This is the first code I ever wrote in jQuery.
$('.expositie, .right img').each(function(){
if ($(".expositie").height() > $(".right img").height()) {
$(".right img").addClass("toggle");
}
});
You could use addClass function parameter too, some find it less readable though:
$('.expositie').find('.right img').addClass(function(){
return $(this).closest(".expositie").height() > $(this).height() ? "toggle" : "";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With