Skip to content

Instantly share code, notes, and snippets.

@painedpineapple
Created September 9, 2015 22:30
Show Gist options
  • Save painedpineapple/9aa98aaedc6bea4b18f2 to your computer and use it in GitHub Desktop.
Save painedpineapple/9aa98aaedc6bea4b18f2 to your computer and use it in GitHub Desktop.
Auto adds alt text to HTML <img> based upon the filename found in the src. jQuery required.
let fileNameFinder = /([\w\d-]+.jpg|[\w\d-]+.png)/;
let altTextFinder = /(.*)\.[^.]+$/;
$('img').each(function() {
if(!$(this).attr('alt')) {
let fileName = $(this).attr('src').match(fileNameFinder)[0];
let altText = fileName.match(altTextFinder)[1];
$(this).attr('alt', altText);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment