JS:
$(function(){
$(\"#example\").popover({
placement: \'bottom\',
html: \'true\',
title : \'
For anyone who is still a little confused:
Here is how it works to toggle the popover after you have gotten it to show, select the same button you used to trigger it and call .popover('toggle') on it.
In this case, for closing the popover the code would be:
$('#example').popover('toggle');
The trick is to get the current Popover with .data('bs.popover').tip():
$('#my_trigger').popover().on('shown.bs.popover', function() {
// Define elements
var current_trigger=$(this);
var current_popover=current_trigger.data('bs.popover').tip();
// Activate close button
current_popover.find('button.close').click(function() {
current_trigger.popover('hide');
});
});
Just wanted to update the answer. As per Swashata Ghosh, the following is a simpler way that worked for moi:
HTML:
<button type="button" class="btn btn-primary example">Example</button>
JS:
$('.example').popover({
title: function() {
return 'Popup title' +
'<button class="close">×</button>';
},
content: 'Popup content',
trigger: 'hover',
html: true
});
$('.popover button.close').click(function() {
$(this).popover('toggle');
});
This works with multiple popovers and I also added a little big of extra JS to handle the z-index issues that happen with overlapping popovers:
http://jsfiddle.net/erik1337/fvE22/
JavaScript:
var $elements = $('.my-popover');
$elements.each(function () {
var $element = $(this);
$element.popover({
html: true,
placement: 'top',
container: $('body'), // This is just so the btn-group doesn't get messed up... also makes sorting the z-index issue easier
content: $('#content').html()
});
$element.on('shown.bs.popover', function (e) {
var popover = $element.data('bs.popover');
if (typeof popover !== "undefined") {
var $tip = popover.tip();
zindex = $tip.css('z-index');
$tip.find('.close').bind('click', function () {
popover.hide();
});
$tip.mouseover(function (e) {
$tip.css('z-index', function () {
return zindex + 1;
});
})
.mouseout(function () {
$tip.css('z-index', function () {
return zindex;
});
});
}
});
});
HTML:
<div class="container-fluid">
<div class="well popover-demo col-md-12">
<div class="page-header">
<h3 class="page-title">Bootstrap 3.1.1 Popovers with a close button</h3>
</div>
<div class="btn-group">
<button type="button" data-title="Popover One" class="btn btn-primary my-popover">Click me!</button>
<button type="button" data-title="Popover Two" class="btn btn-primary my-popover">Click me!</button>
<button type="button" data-title="Popover Three (and the last one gets a really long title!)" class="btn btn-primary my-popover">Click me!</button>
</div>
</div>
</div>
<div id="content" class="hidden">
<button type="button" class="close">×</button>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
CSS:
/* Make the well behave for the demo */
.popover-demo {
margin-top: 5em
}
/* Popover styles */
.popover .close {
position:absolute;
top: 8px;
right: 10px;
}
.popover-title {
padding-right: 30px;
}
I have struggle with this one and this is the simplest way to do it, 3 lines of js:
$(document).ready(function() {
// This is to overwrite the boostrap default and show it allways
$('#myPopUp').popover('show');
// This is to destroy the popover when you click the title
$('.popover-title').click(function(){
$('#myPopUp').popover('destroy');
});
});
@import "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css";
/* Just to align my example */
.btn {
margin: 90px auto;
margin-left: 90px;
}
/* Styles for header */
.popover-title {
border: 0;
background: transparent;
cursor: pointer;
display: inline-block;
float: right;
text-align: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<button id="myPopUp" class="btn btn-success" data-toggle="popover" data-placement="top" data-title="×" data-content="lorem ipsum content">My div or button or something with popover</button>
As a very simple solution to this, I did the following:
1) Add the following CSS:
.sub_step_info .popover::after {
content:url('/images/cross.png');
position:absolute;
display:inline-block;
top:15px;
right:5px;
width:15px;
text-align:center;
cursor:pointer;
}
2) Set data-trigger="manual"
on the popover trigger element
3) Then based on https://github.com/twbs/bootstrap/issues/16802:
$('[data-trigger="manual"]').click(function() {
$(this).popover('toggle');
}).blur(function() {
$(this).popover('hide');
});
This uses the basis that anywhere on the popover is clickable but by focusing on the cross you'll encourage the behaviour you're after.