Skip to content
Advertisement

Change image when another image is clicked

I am trying to change the big image when another small image is clicked, kind of like a product display on an e-commerce website. But my code doesn’t seem to work.

JavaScript
JavaScript

Advertisement

Answer

Don’t repeat JavsScript code. Instead delegate a click to every .button-container‘s img element:

JavaScript
JavaScript
JavaScript

Another suggestion is not to load the fullsize images if you need thumbnails.
Store the Full-size image inside the data-* attribute of the thumbnail image:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement