Firstly sorry if this seem a dumb question but I am learning javascript so may have messed this up.
I have an image that clickis replaced by nother image in its place.The problem I am trying to worj out is where do i place the second image link.
HTML below
<div class="col-lg-3">
<img id="pbook" onclick="imageChange" class="imagebooks" src="images/pink1.jpg" alt="">
</div>
Javascript incomplete below:
function imageChange (newImage) {
var secondImage= document.getElementById("pbook");
secondImage.style.src = newImage;
}
Thank you for your kind help.
I have tried to create a function that when the image is click by the user is switches to another image. Basically the front image of a book that is then clicked on shows thw bck of the book.
2
Answers
You almost got it right. You need to store the in-active image source somewhere:
You’ll be able to access the
data-src
throughdataset
in javascript, and thus, simultaneously store the in-active image.You can pass the second image link (
images/pink2.jpg
) to the function like the following way: