You may avoid the closure.
In order to attach the hover event to all icons elements you can use:
$(icons.toString()).hover(
In this way you will avoid the for loop at all.
Now the problem: how get the index i?
This step can be solved looking for the index:
var i = icons.indexOf('#' + this.id);
As per comment reported by grzesiekgs you may add a new attribute to each element.
So, the previous selector can be chained with:
.attr('idx', (idx, attr) => {return idx;})
And inside the handler you can use directly the new attribute.
And so the snippet:
var icons = ["#know", "#run", "#think", "#done", "#measure", "#plan", "#resolve"];
var animations = ['knowTl', 'runTl', 'thinkTl', 'doneTl', 'measureTL', 'planTl', 'resolveTl'];
$(icons.toString())
.attr('idx', (idx, attr) => {return idx;})
.hover(
function () {
var i = icons.indexOf('#' + this.id);
var idxAttr = this.getAttribute('idx');
console.log('IN (idxAttr =' + idxAttr + '): animations[' + i + ']=' + animations[i]);
//animations[i].restart()
},
function () {
// var i = icons.indexOf('#' + this.id);
// console.log('OUT: animations[' + i + ']=' + animations[i]);
}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="know">know</p>
<p id="run">run</p>
<p id="think">think</p>
<p id="done">done</p>
<p id="measure">measure</p>
<p id="plan">plan</p>
<p id="resolve">resolve</p>