Skip to content

Instantly share code, notes, and snippets.

@argshook
Last active December 28, 2015 05:29
Show Gist options
  • Save argshook/7450179 to your computer and use it in GitHub Desktop.
Save argshook/7450179 to your computer and use it in GitHub Desktop.
Click outside the element to hide it. I was always having difficulties with this, here's a nice tip. This depends on jQuery, just follow the same logic for any other framework or even vanilla JS.
// change selectors as you need
var selector = '.selector';
$(document).ready(function() {
$(selector).click(function(e) {
$(selector).show();
e.stopPropagation();
});
$('body').click(function(e) {
if($(e.target).closest(selector).get(0) == null ) {
$(selector).hide();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment