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

5 Answers 5

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');
Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

0

simple and easy !

$(".playoff-img img").attr("src", "put new source address here");

Comments

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');

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.