Given the following jQuery
.on()
$('header').on({
mouseenter: function(e) {},
mouseleave: function(e) {},
}, 'li');
var $this = $(this)
mouseenter
mouseleave
mouseenter: function(e) {
// Grabs the list element.
var $this = $(this);
// Gets the sub-menu of the active menu item, if there is one.
var $subMenu = $this.find('.main-menu__sub-menu').first();
if ($subMenu.length) {
// Do something...
}
},
mouseleave: function(e) {
// Perform the same checks, and get the same variables as above...
},
click: function(e) {
// Again, perform the same checks and grab the same variables as above...
}
li
With your expanded description, you could go for something like this:
function getSubmenu(li) {
// Grabs the list element.
var $li = $(li);
// Gets the sub-menu of the active menu item, if there is one.
var $subMenu = $li.find('.main-menu__sub-menu').first();
return $subMenu;
}
$('header').on({
mouseleave: function(e) {
var $subMenu = getSubmenu(this)
// do some stuff...
},
click: function(e) {
var $subMenu = getSubmenu(this)
// do some other stuff...
}
}, 'li');