I found a way to bind single elements in an array with a for loop to event handlers in jQuery.
This guide was useful to push me in that direction:
http://www.foliotek.com/devblog/keep-variable-state-between-event-binding-and-execution/
Now I am one level deeper and I am trying to bind muiltiple elements with the same prefix in an array to event handlers in jQuery.
Here's what works:
var menu=new Array("#power","#services","#cashback","#schedule");
$(document).ready(function(){
$(function() {
for(var i in menu)
{
(function() {
var x = menu[i];
var y = menu[i]+'_menu';
$(x).hover(
function () {
$(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
$(y).show();
},
function () {
$(x).css({ backgroundColor: "#333", color: "#FFF"});
$(y).hide();
}
);
$(y).hover(
function () {
$(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
$(y).show();
},
function () {
$(x).css({ backgroundColor: "#333", color: "#FFF"});
$(y).hide();
}
);
})();
}
});
});
Here's what I really want:
var menu=new Array("#power","#services","#cashback","#schedule");
$(document).ready(function(){
$(function() {
for(var i in menu)
{
(function() {
var x = menu[i];
var y = menu[i]+'_menu';
$(x,y).hover(
function () {
$(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
$(y).show();
},
function () {
$(x).css({ backgroundColor: "#333", color: "#FFF"});
$(y).hide();
}
);
})();
}
});
});
UPDATE ::: Here is the final working implimentation:
var menu=new Array("#power","#services","#cashback","#schedule");
$(document).ready(function(){
for(var i in menu)
{
(function(x, y) {
$(x+','+y).hover(
function () {
$(x).css({ backgroundColor: "#000", color: "#3ABCEF"});
$(y).show();
},
function () {
$(x).css({ backgroundColor: "#333", color: "#FFF"});
$(y).hide();
}
);
})(menu[i], (menu[i] + '_menu'));
}
});