Reputation: 1654
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
Reputation: 318252
You could also use a filter :
$('img').filter(function() {
return $(this).prop('alt').indexOf('Show Picture')!=-1;
}).addClass('image-nav');
Upvotes: 2
Reputation: 207517
You want to use the attribute starts with selector [name^="value"]
$('img[alt^="Show Picture"]').addClass("image-nav");
Upvotes: 0
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