SOLDIER-OF-FORTUNE
SOLDIER-OF-FORTUNE

Reputation: 1654

adding a class to some images based on the alt text using jquery?

how can i add a class to some images based on the alt text using jquery?

here is an example of the image:

<img border="0" src="images/Product/icon/26086_1_.jpg" 
alt="Show Picture 1" onclick="setcolorpicidx_26086(1);" 
style="cursor:hand;cursor:pointer;">

so if the alt text contains "Show Picture" then add a class of image-nav

Upvotes: 1

Views: 426

Answers (3)

adeneo
adeneo

Reputation: 318252

You could also use a filter :

$('img').filter(function() {
    return $(this).prop('alt').indexOf('Show Picture')!=-1;
}).addClass('image-nav');

Upvotes: 2

epascarello
epascarello

Reputation: 207517

You want to use the attribute starts with selector [name^="value"]

$('img[alt^="Show Picture"]').addClass("image-nav");

Upvotes: 0

Manse
Manse

Reputation: 38147

$('img[alt^="Show Picture"]').addClass('image-nav');

Uses the attribute starts with selector

or

$('img[alt*="Show Picture"]').addClass('image-nav');

Uses the attribute contains selector

Upvotes: 0

Related Questions