I'm trying to fix some accessibility errors in a new wordpress site by fixing alt tags. I created a new js file called "fixes.js" and included the following:
$('body img').each(function() {
if ( ! $(this).attr('alt'))
$(this).attr('alt', '');
});
$('body img').each(function() {
if ( ! $(this).attr('alt', 'Blog Image'))
$(this).attr('alt', '');
});
I can see the file is being loaded into the page, but it's not doing it's job, as you can see here:
Any ideas what the issue is?
ifstatement? That condition is never going to be true, becauseattr(x, y)returns a jQuery object, and objects are truthy, and so the negation is always false.if ($(this).attr('alt') != 'Blog Image')..attr('alt', 'Blog Image')sets the attribute, it doesn't compare the attribute.altattribute is the same as an empty string.