I have the following code:
var toggleButton = $('.menu-toggle'),
timerDelay;
toggleButton.on("click", function() {
var elem = $(this),
menu = elem.siblings('ul'),
parent = elem.parent(),
parentParent = parent.parent(),
pageH = $(window).height();
elem.toggleClass('pressed');
menu.stop(true);
clearTimeout(timerDelay);
if (menu.is('.scrollable')) {
menu.css({"max-height": pageH - 80});
}
parent.toggleClass('showed');
if (menu.is('.parent')) {
parentParent.toggleClass('showed');
}
if (menu.is('.hidden')) {
menu.css({"height": "100%", "padding": 5}).toggleClass('hidden showed');
} else {
menu.toggleClass('hidden showed');
if (menu.is('.nodelay')) {
menu.css({"height": 0, "padding": ""});
} else {
timerDelay = setTimeout(function() {
menu.css({"height": 0, "padding": ""});
}, 450);
}
}
});
This is the code for a pop-up menu. The problem is that it requires clicking on a specific button to close it. I'm trying to also make it close whenever the user clicks anywhere on the page.
Is there a way?
Maybe I gave the wrong code. This is another section:
$(document).click(function (e)
{
var container = $("#wrapper");
if (!container.is(e.target) && container.has(e.target).length === 0 && event.target.id!=="menu-toggle")
{
container.addClass("toggled");
}
});
Could I mix it with this?
$(document).mouseup(function (e)
{
var container = $("YOUR CONTAINER SELECTOR");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});