Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created September 19, 2012 07:04
Show Gist options
  • Save martinnormark/3748101 to your computer and use it in GitHub Desktop.
Save martinnormark/3748101 to your computer and use it in GitHub Desktop.
jQuery plug-in for removing a DOM element, when clicking outside it
(function ($) {
$.fn.clickOutsideToRemove = function () {
return this.each(function () {
var $this = $(this);
$(document).on("mouseup.clickOutsideToRemove", function (e) {
if ($this.has(e.target).length === 0) {
$this.remove();
$(document).off(".clickOutsideToRemove");
}
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment