Skip to content

Instantly share code, notes, and snippets.

@warnakey
Created September 3, 2021 01:17
Show Gist options
  • Save warnakey/09b2867b19c9ce53d5ebe869c9ef8326 to your computer and use it in GitHub Desktop.
Save warnakey/09b2867b19c9ce53d5ebe869c9ef8326 to your computer and use it in GitHub Desktop.
Remove GIFs from a collection of images
/*
Let's say you have a series of images, and some are JPGs or PNGs, but some are GIFs too. If you want to remove the GIFs, you can do this.
Let's assume your HTML looked like this:
<div>
<img src="https://sqairz.com/wp-content/uploads/2021/04/freedom-white-silver-gif-600x394.gif">
<img src="https://sqairz.com/wp-content/uploads/2021/04/Sqairz-Shoes_071-600x600.jpg">
<img src="https://sqairz.com/wp-content/uploads/2021/04/freedom-white-silver-gif-600x394.gif">
</div>
*/
var getAllImages = document.getElementsByTagName('img');
for (var i = 0; i < getAllImages.length; i++) {
(function(x) {
var theimage = getAllImages[x];
var thesrcs = getAllImages[x].getAttribute('src');
const substring = "gif";
if (thesrcs.includes(substring)) {
theimage.remove();
}
}(i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment