How to add img alt attribute with jquery

I placed the google map on the page and it adds a lot of img tags without alt attribute, how can I add alt for each img in a div in jquery, which I should do instead:

$('#map_canvas > img[alt]').attr('image'); 

thanks

+7
source share
5 answers

To set alternate text for all images:

 $('#map_canvas > img').attr('alt', 'Alternative text'); 

To set alternate text for images that do not have an alt attribute:

 $('#map_canvas > img:not([alt])').attr('alt', 'Alternative text'); 
+13
source

Try:

 $("#map_canvas > img:not([alt])").attr("alt", "<YOURALTTEXT>"); 
+11
source

This work:

 $('#map_canvas').find('img:not([alt])').attr('alt', 'Alt text'); 
+4
source

You can do:

 $('#map_canvas > img[alt=""]').each(function() { $(this).attr('alt', $(this).attr('src'); }); 

This will find all the images in #map_canvas that do not have an alt tag, and then set the alt tag to the same as the src image.

As an alternative, I would recommend:

 $('#map_canvas > img[alt=""]').attr('alt', 'Alt text'); 

Because this will only add alt tags to images that don't have them.

+3
source
 $('#map_canvas > img').attr('alt', 'alt_text'); 
+2
source

All Articles