Skip to content

Instantly share code, notes, and snippets.

@evansd
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save evansd/10744271 to your computer and use it in GitHub Desktop.

Select an option

Save evansd/10744271 to your computer and use it in GitHub Desktop.
Adds a 'clicktoclose' option to Bootstrap's popovers which will cause them to close if the user clicks anywhere outside of the popover
// Adds a 'clicktoclose' option to Bootstrap's popovers which will cause them
// to close if the user clicks anywhere outside of the popover
(function() {
var closeOnExternalClick = function(e) {
if ( ! this.$tip.has(e.target).length) { this.hide(); }
};
var Popover = $.fn.popover.Constructor.prototype;
var superInit = Popover.init;
Popover.init = function() {
superInit.apply(this, arguments);
if (this.options.clicktoclose) {
var clickHandler = $.proxy(closeOnExternalClick, this);
this.$element.on('shown.bs.popover', function() {
$(document).on('click', clickHandler);
});
this.$element.on('hide.bs.popover', function() {
$(document).off('click', clickHandler);
});
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment