Skip to content

Instantly share code, notes, and snippets.

@ruslanchek
Last active August 29, 2015 14:13
Show Gist options
  • Save ruslanchek/b62616ccd7c9979a0ce7 to your computer and use it in GitHub Desktop.
Save ruslanchek/b62616ccd7c9979a0ce7 to your computer and use it in GitHub Desktop.
Click outside JS class
var UIClickOutside = function(container, onClickOutside){
var $container = $(container);
this.bind = function(){
$(document).on('mouseup.UIClickOutside', function (e){
if (!$container.is(e.target) && $container.has(e.target).length === 0){
if(onClickOutside) onClickOutside(e.target);
}
});
};
this.unbind = function(){
$(document).off('mouseup.UIClickOutside');
};
};
// Usage example
var clickOutside = new UIClickOutside('.popup', function($clickedTarget){
// Hide your block or anything else...
});
// Activate
clickOutside.bind();
// Deactivate
clickOutside.unbind();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment