0

I have 3 images all within the same class name "image"

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="imageHolder" style="width: 80%; display:  inline;">
 <div class="image">
  <img src="//i.stack.imgur.com/nO2hl.png">
 </div>
 <div class="image">
  <img src="//i.stack.imgur.com/IkjJW.png">
 </div>
 <div class="image">
  <img src="//i.stack.imgur.com/QrKSV.png">
 </div>
</div>

I would like to replace only the 2nd image with a different image link. So I tried something like this:

$('.image').find('img').attr('src', 'http://cdn.shopify.com/s/files/1/1061/1924/products/Smiling_Face_Emoji_with_Blushed_Cheeks_grande.png?v=1480481056');

However, this replaces all the images and not just the 2nd one so I tried to do $('.image')[1].find(..)..., but that didn't work I was getting the error: Uncaught TypeError: $(...)[1].find is not a function.

I would greatly appreciate it if someone can please explain what I'm doing wrong and how I can fix this?

Thanks.

newbie
  • 1,551
  • 1
  • 11
  • 21

1 Answers1

1

You are quite close - find() will return an array and you want your changes to be applied at a single element:

$($('.image').find('img')[1]).attr('src', 'http://cdn.shopify.com/s/files/1/1061/1924/products/Smiling_Face_Emoji_with_Blushed_Cheeks_grande.png?v=1480481056');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="imageHolder" style="width: 80%; display:  inline;">
 <div class="image">
  <img src="//i.stack.imgur.com/nO2hl.png">
 </div>
 <div class="image">
  <img src="//i.stack.imgur.com/IkjJW.png">
 </div>
 <div class="image">
  <img src="//i.stack.imgur.com/QrKSV.png">
 </div>
</div>

https://api.jquery.com/find/

messerbill
  • 5,499
  • 1
  • 27
  • 38