I need to know the index of clicked element. Can\'t figure out how to do it
for (i = 0; i < document.getElementById(\'my_div\').children.length; i++) {
I had the same issue where I needed to loop through an array and get the index number of the item clicked.
Here is how I solved the issue...
//first store array in a variable
let container = [...document.getElementById('container')];
//loop through array with forEach function
container.forEach((item,index) => {
item.addEventListener('click', () => console.log(index));
});
This will console.log the index number of the item clicked on.
Hope this answers some questions.