"Popover closes on unfocus"
Bootstrap 3.2.0 Snippet by diglididudeng

1
2
3
4
5
6
7
8
9
10
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<p><a id="YOUR_BUTTON_FOR_POPOVER" class="btn btn-primary" role="button" data-toggle="popover" href="#">Click me</a>, then click somewhere else afterwards !</p>
</div>
</div>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
body {
display: block;
position: relative;
width: 100%;
height: 800px;
background-color: #ddd;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$(document).ready(function()
{
// Example popover.
// Replace it with your own implementation.
$('#YOUR_BUTTON_FOR_POPOVER').popover({
animation: true,
placement: 'bottom',
trigger: 'focus',
title: 'See?',
content: 'Now try clicking outside !'
}).on("click", function() {
$(this).popover("show");
})
// Where the magic happens !!!
//
// NOTE: it does not need you changing anything,
// unless you only want specific popovers to disappear
// after unfocusing.
$('body').on('click', function (e) {
//only buttons
if ($(e.target).data('toggle') !== 'popover'
&& $(e.target).parents('[data-toggle="popover"]').length === 0
&& $(e.target).parents('.popover.in').length === 0) {
$('[data-toggle="popover"]').popover('hide');
}
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments: