0

I got the following code:

HTML

<div class="image">
    <img src="http://ecx.images-amazon.com/images/I/41ghiuvjdvL._SL75_SS50_.jpg" />
</div>

<br />

<div class="photo">
    <img src="http://profile.ak.fbcdn.net/hprofile-ak-ash2/27520_119569768054263_2432_q.jpg" />
</div>​

JS

// getting src of bottom image
var bottomSrc = $(".photo img").find("src");

// getting src of top image
var topSrc = $(".image img").find("src");

$(".photo").click(function() {
    // changing sources
    // topSrc.attr("src", bottomSrc);
    // topSrc.replaceWith(bottomSrc);
    topSrc.replace(bottomSrc);
})​

Fiddle

When the bottom image is clicked, I want the src of the top image to be replaced with the bottom image's src, so that both images are the same.

Why does the supplied code does not work?

1
  • you want to use .attr('src') to get the value of the src attribute, not .find() which finds child elements Commented Jul 24, 2012 at 23:53

2 Answers 2

4

Try this out:

$(".photo").click(function() {
    var src=$(this).children("img").attr("src");
    $(".image img").attr("src",src);
})​;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

You need to get and set the src attribute, using attr() function in jQuery

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.