webmasters
webmasters

Reputation: 5831

Jquery code to resize images not working

I am a total jquery newb and I have a deadline for this project so please don't laugh.

I am trying to make this jquery code work:

function resize_images(){
   var def = $(".defx img").height(); // get the desired height

   $(".model").each(function() { // loop through all images inside model divs
       var images = $(this).find("img"); // find the image
       var height = images.height(); // find the image height
       if (height > def){  images.css("height: "+def+"px !important"); } // if the image height is larger than the default add a css rule
   });  
}

Any ideas why it is not working?

Upvotes: 1

Views: 86

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219920

You're only checking the height of the first img. Loop over the images themselves:

function resize_images() {
    var maxHeight = $(".defx img").height();
    $(".model img").each(function() {
        var $this = $(this);
        if ( $this.height() > maxHeight ) {
            $this.css('height', maxHeight);
        }
    });
}

Upvotes: 1

Related Questions