Skip to content

Instantly share code, notes, and snippets.

@imliam
Created March 17, 2017 16:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imliam/06e0bb4d6d7485cd0fb4eab3ca096d86 to your computer and use it in GitHub Desktop.
Save imliam/06e0bb4d6d7485cd0fb4eab3ca096d86 to your computer and use it in GitHub Desktop.
Bootstrap 4 - Close Popover When Losing Focus
/*
|--------------------------------------------------------------------------
| Bootstrap 4 - Close Popover When Losing Focus
|--------------------------------------------------------------------------
|
| A JavaScript snippet that closes a Bootstrap 4 popover when clicking off
| of it, but unlike the default behaviour, allows it to stay open when
| clicking within the popover itself.
*/
$(function(){
$("[data-toggle=popover]").popover();
$('body').on('click', function (e) {
$('[data-toggle="popover"]').each(function () {
if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
$(this).popover('hide');
}
});
});
});
@kbk007
Copy link

kbk007 commented Jan 23, 2022

Very useful to me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment