Created
March 17, 2017 16:42
-
-
Save imliam/06e0bb4d6d7485cd0fb4eab3ca096d86 to your computer and use it in GitHub Desktop.
Bootstrap 4 - Close Popover When Losing Focus
This file contains 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
/* | |
|-------------------------------------------------------------------------- | |
| 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'); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful to me. Thanks.