When users toggle the $('#menu'); button, they have no problem. However, when users mouseleaves the $('#dropdown'); then click back the $('#menu'); they have to click twice in order for the toggle to work again. Am I doing something wrong?
===========HTML===========
<a class="" href="javascript:;" id="menu"></a>
<div style="display: none;" id="dropdown">
<div id="wrap">
<div class="menu-section">
<div class="menu-header-section">
<h3>Header</h3>
</div>
<div class="menu-list">
<ul class="list">
<li><a href="#">link</a></li>
<li><a href="#">link</a></li>
</ul>
</div>
</div>
</div>
<a href="javascript:;" class="closeThis">close</a> </div>
</div>
===========jQuery===========
//menu
var menu = $('#menu');
var dropdown = $('#dropdown');
menu.toggle(function () {
menu.addClass('active');
dropdown.fadeIn();
}, function () {
menu.removeClass('active');
dropdown.fadeOut();
return false;
});
dropdown.mouseleave(function () {
menu.removeClass('active');
dropdown.fadeOut();
return menu;
});
The docs explain it all:
In other words,
toggle()alternates based on even and oddclickevents. So:click [even]: Hide
click 1: Show
#dropdownis already hidden here, but regardless, it’s an even click. Click once, and you’re hiding it again.Anyways, that’s the explenation for your problem. Working on solution.
Solution
Manually trigger the
clickevent.Replace:
With:
Demo: http://jsfiddle.net/aymansafadi/BF8p6/2/