I'm importing children of #parentEle, making a copy of each and pushing that copied object to array with new ID - #eleCopy(i)
I'm trying to also assign a click function to the original object imported. Clicking it would change CSS of its copy (referenced from the array). But the copy being affected is always the same one - last one loaded. I was hoping to reference it by its index number in the array... How do I "freeze" correct array reference in the click function so that $(this) 0 would corresponds to ar[0], $(this)` 1 to ar[1] and so on?
var ar = [];
var i = 0;
$('#parentEle').children().each(function() {
... // copy of $(this) is created: $('#eleCopy'+i)
ar.push($('#eleCopy'+i));
$(this).on('click', function() {
ar[i].css({ ... });
});
i++;
});