I'm creating my own lightbox gallery, basically what I need to do is handle the onclick event. So let's say there's a series of <img class="galleryItem" src="path">
the onclick event will pass the index of clicked image to slide changing function (yes I know I can just use $('galleryItem').click()
in JQuery but I'm trying to do this in vanilla JS.
I came along a for loop to indicate which class member has been clicked but the script stops to work cause the loop just ends. The code was like:
var members = document.getElementsByClassName('galleryItem');
for(var i = 0; i < memebers.length; i++) {
members[i].onclick = function memberOnclick() {
console.log(i)
}
}
I also tried members.addEventListener("click", memberOnclick);
in for loop but still can't get it to return the index of clicked member. I can imagine the solution to this problem is easy but I kinda out of ideas how to make this work