I have a multiple menu's on my page which all use the same mouseover and click events, so I decided to get it into a function. However vars seem to always be assigned to the last of the arguments for the hover(function, function) function.
$(document).ready( function() {
menuMouseOver = function() {
for(i=0, u=arguments.length; i<u; i++){
var parent = arguments[i].parent;
var active = arguments[i].active;
var childSelect = arguments[i].childSelect;
console.log(active); //logs the correct active
$(parent).children(childSelect)
.not('.'+active).each( function(i, e) {console.log(active);})
//The above console.log logs the correct active
.hover( function() {
console.log(active); //this one always logs menu2_active
$(this).addClass(active);
}, function() {
$(this).removeClass(active);
});
}
}
menuMouseOver( { parent: '#menu1',
active: 'menu1_active',
childSelect: ':gt(0)'},
{ parent: '#menu2',
active: 'menu2_active',
childSelect: ':gt(0)'});
});
Why is is that the last console.log will always logs the last active instead of the one that belongs to arguments[i].active. (In this example it always logs the active of arguments[1].active) What am I doing wrong?
Also, the real function is more complex, but the problem is also present in this variant.