Skip to content

Instantly share code, notes, and snippets.

@ariona
Created April 8, 2016 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariona/c8617a2d07623e8c53dcf0d333694f8e to your computer and use it in GitHub Desktop.
Save ariona/c8617a2d07623e8c53dcf0d333694f8e to your computer and use it in GitHub Desktop.
Simulate Click Outside Element

Simple plugin for detecting click outside element

$.fn.clickOff = function(callback, selfDestroy) {
var clicked = false;
var parent = this;
var destroy = selfDestroy || true;
parent.click(function() {
clicked = true;
});
$(document).click(function(event) {
if (!clicked) {
callback(parent, event);
}
if (destroy) {
//parent.clickOff = function() {};
//parent.off("click");
//$(document).off("click");
//parent.off("clickOff");
};
clicked = false;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment