jQuery Tricks Quickie – Select Images by using Alt tags

In jQuery you have multiple selectors to either select single objects or a whole bunch. I needed to select an image by its alt tag, and guess what, there is also a selector for alt tags. You need to do the following:

$(‘img[alt=”My Alt Image”]’)

I needed this so that I could change the class and set the selected image as active, by searching the alt tag as below:

$(‘img[alt=”My Alt Image”]’).addClass(‘.active’);

Something else that might be useful is to revert the active image, to a normal image. Instead of using the alt tag this time you can use the ‘active’ class selector as below:

$(‘.active’).removeClass(‘active’);

Hope this was of help. Buon selectoring 😉

Hello, nice to meet you

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Leave a Reply