Multiple img onclick function | Sololearn: Learn to code for FREE!


Multiple img onclick function

Hello, I tried to use multiple onclick tunction for each image but its not working. So if the image is clicked, it should pop up and gets bigger. Is it possible to use a switch case statement or something to check which image is clicked? Thank you! Here is the code:

4/7/2019 12:34:05 PM


4 Answers

New Answer


Use a css class for all the displayed images, populate it with the same onclick event. You only need one set of modal image container. The modal would be updated with correct image and capture. I simplified your code here with the same event function, but populate it to all image elements. Practice DRY, don't repeat yourself on coding always would avoid unnecessary errors.


Thanky you!


There is no span with class="close", which causes the error.


Sry i just forgott this part of the code. Here is the reworked code: