2

I'm trying to replace the src of an image and I currently have this:

if(('product-img img').src = "/webimages/blank.gif") {
   $(".product-img img").attr("src","/images/blank.gif");
}

It is working but it's happening to all the images on the page. I only want it to happen if the image src = /webimages/blank.gif. I've also tried $(this).attr("src","/images/blank.gif"); anyone know what I'm missing? Thanks

3
  • 3
    have doubt... if(('product-img img').src = "/webimages/blank.gif") , here '$' missing also 'product-img' is what class the '.' (dot) missing. Commented Dec 16, 2015 at 3:51
  • Condition should be: if($('.product-img img').attr('src') == "/webimages/blank.gif") { Commented Dec 16, 2015 at 3:51
  • 2
    For starters, you need to use == not =. javacoffeebreak.com/faq/faq0022.html Commented Dec 16, 2015 at 3:53

3 Answers 3

6

Try this:

$('.product-img img[src="/webimages/blank.gif"]').attr("src","/images/blank.gif");
Sign up to request clarification or add additional context in comments.

2 Comments

@AlexMurray, good to know ... if it helped please select as best answer :)
will do! you answered too fast still need to wait 2mins :)
0

You are missging =, change = to ==, the code should as follow:

if($('.product-img img').src == "/webimages/blank.gif") {
   $(".product-img img").attr("src","/images/blank.gif");
}

Comments

0

Do it like this

if(('img.product-img').src == "/webimages/blank.gif") {
   $("img.product-img").attr("src","/images/blank.gif");
}

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.