I'm looping through a counter, finding elements based on the counter value, and binding events to that set that matches the counter.
psuedo code:
for index in counter
find elements of data attribute matching counter
attach click event to that matched query set
js:
var groups = [];
$('.match-me').each(function(){
var group_id = $(this).data('instance');
if(groups.indexOf(group_id) < 0){
groups.push(group_id);
}
});
for(var i of groups ){
window['test'+i] = $('*[data-instance=' + i + ']');
window['test'+i].each(function(){
$(this).on('click',function(){
//CONSOLE.LOG OF window['test'+i] OUTPUTS THE LAST MATCHED SET
window['test'+i].css('color','red');
});
});
}
The issue is inside where I am binding the event listening, the variable for the matched set of elements is always the last matched set of elements. I thought that by creating dynamic global variables in window
I would escape this problem, but I'm not sure what I'm doing wrong.