JQuery get every image with 100px or more width

81 views Asked by At

How do I get the length of every image with 100px or more of width:

if( $('img').width( >= 100 ).length > 0 ){
   [MORE CODE]
}
5

There are 5 answers

1
Radonirina Maminiaina On BEST ANSWER

You can do this:

$('img').each(function() {
    if( $(this).width() >= 100 ){
       [MORE CODE]
    }
});
0
AmmarCSE On

Use jQuery filter() like

$('img').filter(function(){
    return $(this).width() >= 100;
});
0
Rory McCrossan On

You can use filter() to achieve this:

$(window).load(function() {
    var $imgs = $('img').filter(function() {
        return $(this).width() >= 100;
    });

    // do something with $imgs...
});

Note that you should execute this under the window.load event to ensure that the img elements have loaded and therefore have a set width.

0
D4V1D On

Try

$('img').each(function() {
     if($(this).width() >= 100) {
         // more code
     }
});
2
Praveen Kumar Purushothaman On

Use the outerWidth to determine:

$(window).load(function() {
    var $imgs = $('img').filter(function() {
        return $(this).outerWidth() >= 100;
    });

    // do something with $imgs...
});