Jquery addclass if image has specific alt tag

I have some images, all of which have alt tags.

I am trying to add a class to all images that have a specific alt tag.

So, to express this in simple terms:

If the image is alt tag = "dog", then add the class "canine"

Any ideas?

thanks

+4
source share
3 answers

You can use the attribute equal to the selector and addClass :

 $("img[alt='dog']").addClass("canine"); 
+11
source

or you can use

 $("img[alt*='dog']").addClass("canine"); 

include any image with alt containing dog .

+8
source
 $('img[alt="dog"]').addClass("canine") 

it is very simple ...

notetoself: type faster ...;)

+3
source

All Articles