Retrieve last pro gallery item clicked outside of onItemClick function

Hi everybody,

I am trying to use a pro gallery as an option selector for users to choose between 3 options that would correspond to the 3 images in the gallery.

So I used this code in order to get the Item index of the last item clicked.

$w ( “#gallery3” ). onItemClicked (( event ) => {
itemIndex = event . itemIndex
console . log ( itemIndex )
})

So it works because the console shows me alternatively 0, 1 or 2 according to what item is clicked. However I can’t seem to be able to retrieve the variable outside the function in order to use it in other functions, in my case that would be mainly in the addtocart product options.

Would there be way to make the itemIndex variable usable outside of the function ?

Thank you in advance !

Try using “var” for the variable outside the function and see if that scope works and try and look at it as a global variable. Also look at https://www.wix.com/velo/reference/wix-storage

Good luck

Hi Kyle I had used var outside the function but it did not work. However I had no idea about wix-storage and it works perfectly for this purpose, thank you so much!

@tberthe yay!