javascript - jQuery Toggle on mouseup -
i have following jquery:
$('#account-menu').hide(); $("#account-link").click(function(e) { e.preventdefault(); $("#account-menu").toggle(); $("#account-link").toggleclass("selected"); });
what want check if user clicks anywhere else onscreen whilst account menu shown not within account menu itself, , if hide menu. can help?
thanks
edit: had go @ doing myself so:
$('#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 link or box, exit if ($targ.is('#account-link') || $targ.is('#account-menu')) return; // if have parent either, exit if ($targ.closest('#account-link, #account-menu').length) return; // hide box, unselect link $("#account-link").removeclass("selected"); $("#account-menu").hide('fast'); });
but wanted see if there nicer , smaller (code-wise) way of doing it.
Comments
Post a Comment