Last active
August 29, 2015 13:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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