- This function checks whether each image tag that contains alternative text or not. If it does not exist, then it will add ‘alt’ attribute.
- The value for that attribute will be the name of the image which fetches from the URL of the image.
<script>
$(document).ready(function() {
$('img').each(function(){
var $img = $(this);
var filename = $img.attr('src');
var attr = $(this).attr('alt');
if (typeof attr == typeof undefined || attr == false) {
$img.attr('alt', filename.substring((filename.lastIndexOf('/'))+1, filename.lastIndexOf('.')));
}
});
});
</script>
Advantages of this method compared with previous methods
- No need of editing each image tag section.
- A single function added to the header will automatically create the alternative text.
Disadvantages of this method compared with previous methods
- The auto-created alternative text name will be the name of the image. So, whatever the image name, the same will be reflected in its alternative text.
- Custom modification for alternative text is not possible.