I need to show/hide a cascading menu when the user move the mouse on a certain element. This is quite easy to achieve with jquery, using the hover function:
N.B: The timer is used only to hide the menu after 200ms, and is not important.
$(document).ready(function() {
var timer;
$('.element,.cascading_menu').hover(function(){
clearTimeout(timer);
$('.cascading_menu').show();
}, function(){
timer = setTimeout(function(){$('.cascading_menu').hide();},200);
});
});
I have to repeat this code for every menu I want to hide.
But since I have a lot of menus, I would like to reduce the code length.
My idea was to use an array of "id of element hovering:id menu to show/hide".
Do you think it is possible to write a jQuery function that, given this array of elements, provide to show each menu, without having to write that code a dozen of times?