35

I am trying to replace the img source of a given source using jQuery. For example, when the image src is smith.gif, replace to johnson.gif. If williams.gif replace to brown.gif etc.

EDIT: The images are retrieved from an XML to a random order, without class to each .

This is what I tried:

if ( $("img").attr('src', 'http://example.com/smith.gif') ) {
              $(this).attr('src', 'http://example.com/johnson.gif');
            }
if ( $("img").attr('src', 'http://example.com/williams.gif') ) {
              $(this).attr('src', 'http://example.com/brown.gif');
            }

Note that my HTML has many images. For example

<img src="http://example.com/smith.gif">
<img src="http://example.com/williams.gif">
<img src="http://example.com/chris.gif">

etc.

So, how can I replace the images: IF img src="http://example.com/smith.gif" then show "http://example.com/williams.gif". etc...

Thanks alot

5
  • 2
    You are missing closing "s in your HTML example Commented Sep 18, 2011 at 14:29
  • 2
    You're using $(img) for checking the property, and $(this) for setting it. Is this really what you had in mind to do? Commented Sep 18, 2011 at 14:29
  • This is not how attr() works. You need to walk through each image using .each() Commented Sep 18, 2011 at 14:30
  • Sorry, I just typed fast the explanation Commented Sep 18, 2011 at 14:30
  • I think you need something more like this. $("img[src=example.com/smith.gif]") Commented Sep 18, 2011 at 14:33

3 Answers 3

82

This is what you wanna do:

var oldSrc = 'http://example.com/smith.gif';
var newSrc = 'http://example.com/johnson.gif';
$('img[src="' + oldSrc + '"]').attr('src', newSrc);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir. This is what I was looking for. Give me 10 minutes to answer your question.
21

You need to check out the attr method in the jQuery docs. You are misusing it. What you are doing within the if statements simply replaces all image tags src with the string specified in the 2nd parameter.

http://api.jquery.com/attr/

A better way to approach replacing a series of images source would be to loop through each and check it's source.

Example:

$('img').each(function () {
  var curSrc = $(this).attr('src');
  if ( curSrc === 'http://example.com/smith.gif' ) {
      $(this).attr('src', 'http://example.com/johnson.gif');
  }
  if ( curSrc === 'http://example.com/williams.gif' ) {
      $(this).attr('src', 'http://example.com/brown.gif');
  }
});

1 Comment

Thank you sir. I will be using Niko's example. Less code if you won't use the var OldSrc and newSrc.
5

In my case, I replaced the src taq using:

   $('#gmap_canvas').attr('src', newSrc);

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.