views:

664

answers:

2
$(document).ready(function() {
var pic = $(".pic");

// need to remove these in of case img-element has set width and height
$(".pic").each(function() {
    var $this = $(this);
    $this.removeAttr("width"); 
    $this.removeAttr("height");

    var pic_real_width = $this.width();
    var pic_real_height = $this.height();
    if(pic_real_width<100){
    $(this).css("display","none");
    }
    });

 });

Solved thanks to the kind people here at stackoverflow. As I said I've added an if function to check if the width is under 100px. For some reason it hides everything. Anyone know why?

+4  A: 

You're using pic when you should be using $(this):

$(".pic").each(function() {
    var $this = $(this);
    $this.removeAttr("width"); 
    $this.removeAttr("height");

    var pic_real_width = $this.width();
    var pic_real_height = $this.height();
    alert(pic_real_width);
     });

You should also watch for images that are resized with CSS.

Instead of

    $this.removeAttr("width"); 
    $this.removeAttr("height");

try

$this.css({width: 'auto', height: 'auto'});
Greg
+1  A: 

Try this:

$(document).ready(function() {
    $(".pic").each(function() {
     var pic = $( this );
     pic.removeAttr("width"); 
     pic.removeAttr("height");

     var pic_real_width = pic.width();
     var pic_real_height = pic.height();
     alert(pic_real_width);
    });
});
Jan Hančič
It worked, thanks. var pic_real_width = $this.width(); var pic_real_height = $this.height(); if(pic_real_width<100){ $(".pic").css("display","none"); }Hides all images. Any ideea why it doesn;t apply to that particular condition?
Carvefx
see my answer to your latest question: http://stackoverflow.com/questions/1873536/jquery-height-condition/1873546#1873546I've answered you there.
Jan Hančič