jessica
jessica

Reputation: 495

Set image source to another image

I have two images, and i want to set the src of the first image to the second one, knowing that the source will keep on changing i can't keep it the same, how can i do that? here is what i have tried so far:

$("#image2").attr("src","/images");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="image1" width="50" height="50" src="/images">
 <img id="image2" width="50" height="50" src="">

Upvotes: 0

Views: 1444

Answers (2)

mplungjan
mplungjan

Reputation: 177685

Use onload on the first image. I did this inline since we need to catch the onload on the tag and not after load. This will change the second image whenever the first image changes

<img id="image1" width="50" height="50" 
src="http://lorempixel.com/output/sports-q-c-50-50-1.jpg" 
onload="document.getElementById('image2').src=this.src">

<img id="image2" width="50" height="50" src="">

Upvotes: 5

Esko
Esko

Reputation: 4207

If I understood you correctly, you want to read the src-attribute from the first image and set it to the second image? Then you can simply do:

$("#image2").attr("src", $("#image1").attr("src"));

Upvotes: 0

Related Questions