Loop through class with if statement

Loop through class with if statement

Hi all,

I have multiple divs with class .repository_image all containing an image. I need to loop through them and assign the class .portrait if it contains an image that is in portrait orientation.

I tried the following but it did not work :(

  1. $('.repository_image').each(function(){
  2.     if($(this).width() < $(this).height()){
  3.         $(this).addClass("portrait");
  4.     }
  5.  });

What am I doing wrong?