Skip to content Skip to sidebar Skip to footer

Specific Image Extension Selector In CSS Or JQuery

I have several images in my project with several extensions (jpg, png, gif). Is there any way to select these images according to their extensions in css or JQuery. ).height(200);

The ends with selector will inaccurately miss <img src="test.png?id=1">.

You could also use the attribute contains selector, *=:

$('img[src*=".png"]').height(200);

The contains selector will inaccurately select <img src="test.png.gif" />.

If file path is all you have to go by, you'll unavoidably have to accept one of these exceptions (unless you want to implement some more advanced filter that strips values after ? or #).


Solution 2:

You can also use CSS3 attribute selectors:

img[src$='.png'] {
    height: 200px;
}

Browser support

Chrome    Firefox               IE     Opera     Safari
All       1.0 (1.7 or earlier)  7      9         3

Solution 3:

To select all of them, you need to use .filter():

var images = $('img').filter(function() {
    return /\.(png|jpg|gif)$/.test(this.src);
}).height(200);

Post a Comment for "Specific Image Extension Selector In CSS Or JQuery"