So I've been trying to organize my jQuery code (which currently has everything in $(document).ready()
) by implementing an object literal on the #Menu
. But now the event delegation that used to work is no longer working.
HTML
<div id="Menu">
<header id="Menu-Header">
<button id="Menu-Button" type="button" class="btn btn-default btn-lg button-menu" aria-label="Menu">
<span class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span>
<h1>Problems</h1>
</button>
</header>
...
</div>
jQuery
$(document).ready(function () {
menu.init();
...
});
var menu = {
$menu: $('#Menu'), $menuButton: $('#Menu-Button'),
init: function () {
this.$menuButton.on('mouseenter', function () {
this.$menuButton.children('.glyphicon-menu-hamburger').hide();
this.$menuButton.children('h1').fadeIn(200);
});
this.$menuButton.on('mouseleave', function () {
this.$menuButton.children('h1').hide();
this.$menuButton.children('.glyphicon-menu-hamburger').fadeIn(200);
console.log("is this even working");
});
console.log("menu init being called");
}
};
And while menu init being called
is being printed, the other print statement doesn't show up, which I feel indicates that .on()
is not being initialized correctly.
I feel like I may be making a selector typo or something, but I can't seem to figure it out.
If anyone could help me with this that would be greatly appreciated!