I have the following jQuery:
$('#account-menu').hide();
$("#account-link").click(function(e) {
e.preventDefault();
$("#account-menu").toggle();
$("#account-link").toggleClass("selected");
});
What I want to do is check that the user clicks nowhere on the screen AFTER INDICATING THE ACCOUNT MENU , but not inside the account menu itself, and if so, hide the menu. Can anyone help?
thank
EDIT: I myself did it like this:
$('#account-menu').hide();
$("#account-link").click(function(e) {
e.preventDefault();
$("#account-menu").toggle('fast');
$("#account-link").toggleClass("selected");
});
$(document).mouseup(function(e) {
var $targ = $(e.target);
if ($targ.is('#account-link') || $targ.is('#account-menu')) return;
if ($targ.closest('#account-link, #account-menu').length) return;
$("#account-link").removeClass("selected");
$("#account-menu").hide('fast');
});
But I wanted to see if there was a way to make it more beautiful and much smaller (code-wise).
source
share