Created
September 9, 2015 22:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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