0

I have this HTML class where I want to replace the img src with different image. Have been able to find something close but nothing quite working.

HTML:

<div class="playoff-img">
  <a href=""><img src="src/img/imga.png"></a>
</div>

Javascript:

$('div.playoff-img:contains("src/img/imga.png")').replaceWith('src/new/image/imgb.png');
  • 1
    The question is duplicate of [Changing the image source using jQuery](https://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery) – Alexander Jul 07 '18 at 16:05

5 Answers5

2

You need an attribute selector and use .attr to set the src attribute.

$('div.playoff-img img[src="src/img/imga.png"]').attr('src', 'src/new/image/imgb.png');
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

The Js way of solving this problem

document.getElementsByTagName("img")[0].setAttribute("src","http://www.gstatic.com/webp/gallery/4.jpg");

The jQuery way of solving this problem

$(".playoff-img img").attr("src", "http://www.gstatic.com/webp/gallery/4.jpg");
Rando Hinn
  • 1,255
  • 19
  • 41
Dev
  • 11
  • 1
0

simple and easy !

$(".playoff-img img").attr("src", "put new source address here");
MajiD
  • 2,420
  • 1
  • 22
  • 32
0

Use [attr*="value"] to select an attribute that contains a particular string. Next you will want to use attr() to set the attribute to something new.

$('div.playoff-img img[src*="src/img/imga.png"]')
    .attr('src', 'src/new/image/imgb.png');
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0
<!-- use jquery -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="playoff-img">
  <a href=""><img src="src/img/imga.png"></a>
</div>

<script>
$(function(){
    $(".playoff-img").attr("src", "put new source address here");
});
</script>