I am working on a project where I have two sets of identical images - one is displayed in a gallery format and the other set is hidden until its corresponding gallery image is clicked, creating a lightbox effect.
For example:
<ul id="gallery">
<li><img class="gallery-image" src="dog"></li>
<li><img class="gallery-image" src="cat"></li>
<li><img class="gallery-image" src="rabbit"></li>
</ul>
<ul id="modal">
<li><img class="modal-image" src="dog"></li>
<li><img class="modal-image" src="cat"></li>
<li><img class="modal-image" src="rabbit"></li>
</ul>
I have written a script that loops over the gallery images and adds an event listener to each image. The goal is to be able to add a class to the corresponding "modal-image" when a "gallery-image" is clicked.
Both sets of images are in the same order and have matching indexes when retrieved as nodelists using document.getElementsByClassName (with different variable names like galleryImage[0] and modalImage[0]). My question is: Is there a method to automatically add a class to the corresponding "modal-image" when clicking a "gallery-image" using the nodelist?
If anyone has a solution, I would appreciate a detailed explanation of the logic behind it. I have seen examples that involve assigning IDs with index numbers to each image, but I would like to keep all the JavaScript in one file and understand how to work with nodelists effectively.
Thank you for your help!