I have the code below, which is working (i.e. I can print the index to the console). However, I’d like to use that index outside of the forEach loop. I have tried random things like returning the tabIndex, wrapping it all in a function etc. but I don’t get it right or understand the steps needed.
The plan is to use the index to fetch data from a JSON file.
Any suggestions on how to make the result from tabIndex globally available?
/* DESTINATION TABS */
const destinationTabs = document.querySelectorAll(".destination-info--tab");
/* Get index of the clicked tab */
destinationTabs.forEach((destinationTab) => {
destinationTab.addEventListener("click", (e) => {
const tabIndex = Array.from(destinationTabs).indexOf(e.target);
console.log(tabIndex);
});
});