Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an alt attribute of img tags with jquery

I've put a google map in a page and it makes lots of img tags without the alt attribute, how can I add the alt for each img in a div in jquery, what's I gotta make instead of :

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

Thanks

like image 562
user1301411 Avatar asked May 02 '12 13:05

user1301411


3 Answers

You could do:

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

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

Alternatively, I would recommend:

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

As this will only add alt tags to images that don't have one.

like image 145
Richard Avatar answered Sep 29 '22 20:09

Richard


$('#map_canvas > img').attr('alt', 'alt_text');
like image 44
Bibin Velayudhan Avatar answered Sep 29 '22 20:09

Bibin Velayudhan


To set alternative text to all the images:

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

To set alternative text to images that does not have alt attribute:

$('#map_canvas > img:not([alt])').attr('alt', 'Alternative text');
like image 30
VisioN Avatar answered Sep 29 '22 19:09

VisioN