I'd like to assign several "click" eventlisteners to several elements that I create using a "for" loop. The unique information for each of these elements is stored in a dictionary, and I can access it using the index of the for loop. What I'd like to do, then, is to have the eventlisteners assigned to these buttons retain the index (or the value of the dictionary associated with the index, but it amounts to the same thing), because at the moment I'm only getting what appears to be the loop index when the loop has finished.
Here's the simplified code;
for (var ind = 0; ind < JSON.parse(localStorage.getItem('data')).length; ind++) {
var sc = document.createElement('div');
sc.addEventListener('click', function () {
console.log(ind);
})
}
I tried googling for answers but couldn't find anything, then I tried putting the index in the parentheses of the event listener function but it returns a strange pointer instead.
Something like PointerEvent {...}
with a series of attributes.