-4

I'm trying to get the width and height of certain images equal (or, more specific: the div around it), but somehow it doesn't seem to work.. Anyone who can help?

$('.a2paragraph .image img').each(function() {
    var img = $('.a2paragraph .image img');

    var h = img.height(),
        w = img.width();

    if (h > w) {
        img.parent().css('height', w);
    }
    else if (w < h) {
        img.parent().css('width', h);
    }


});

EDIT: changed the code, but still not working:

$('.a2paragraph .image img').each(function() {
    var img2 = $('.a2paragraph .image img');

    var h = img2.height(),
        w = img2.width();

    if (h > w) {
        img2.parent().css('height', w);
    }
    else if (h < w) {
        img2.parent().css('width', h);
    }


})

1 Answers1

5
if (h > w) {
    img.parent().css('height', w);
}
else if (w < h) {
    img.parent().css('width', h);
}

The if and the else if conditions are the same. You're checking for h > w twice, just written in different orders.

Victor Sand
  • 2,270
  • 1
  • 14
  • 32