Mona Coder
Mona Coder

Reputation: 6316

Targeting <img> by alt attribute in jQuery

I am working on the following code. I want to find out why I am not able to target the <img> tag which only has the "award logos" alt

<img src="http://www.....jpg" alt="award logos">
<img src="http://www.....jpg" alt="Ranking">

<script>
$(document).ready(function() {
  $('img [alt="award logos"]').wrap("<a href='http://google.com' </a>");
});
</script>

Technically, what I want to do is targeting all the images that has the specific alt attributes.

Upvotes: 0

Views: 91

Answers (1)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Remove space between img and [alt="award logos"] because of the space it is looking for child element of img with attribute [alt="award logos"].

$(document).ready(function() {
  $('img[alt="award logos"]').wrap("<a href='http://google.com'> </a>");
});

NOTE - you missed > for a (anchor) tag while wrapping img, please correct it.

JSFiddle Demo

Upvotes: 6

Related Questions