JS:
$(function(){
$(\"#example\").popover({
placement: \'bottom\',
html: \'true\',
title : \'
I needed to find something that would work for multiple popovers and in Bootstrap 3.
Here's what I did:
I had multiple elements I wanted to have a popover work for, so I didn't want to use ids.
The markup could be:
<button class="btn btn-link foo" type="button">Show Popover 1</button>
<button class="btn btn-link foo" type="button">Show Popover 2</button>
<button class="btn btn-link foo" type="button">Show Popover 3</button>
The content for the save and close buttons inside the popover:
var contentHtml = [
'<div>',
'<button class="btn btn-link cancel">Cancel</button>',
'<button class="btn btn-primary save">Save</button>',
'</div>'].join('\n');
and the javascript for all 3 buttons:
This method works when the container is the default not attached to body.
$('.foo').popover({
title: 'Bar',
html: true,
content: contentHtml,
trigger: 'manual'
}).on('shown.bs.popover', function () {
var $popup = $(this);
$(this).next('.popover').find('button.cancel').click(function (e) {
$popup.popover('hide');
});
$(this).next('.popover').find('button.save').click(function (e) {
$popup.popover('hide');
});
});
When the container is attached to 'body'
Then, use the aria-describedby to find the popup and hide it.
$('.foo').popover({
title: 'Bar',
html: true,
content: contentHtml,
container: 'body',
trigger: 'manual'
}).on('shown.bs.popover', function (eventShown) {
var $popup = $('#' + $(eventShown.target).attr('aria-describedby'));
$popup.find('button.cancel').click(function (e) {
$popup.popover('hide');
});
$popup.find('button.save').click(function (e) {
$popup.popover('hide');
});
});
$(function(){
$("#example").popover({
placement: 'bottom',
html: 'true',
title : '<span class="text-info"><strong>title!!</strong></span> <button type="button" id="close" class="close">×</button></span>',
content : 'test'
})
$(document).on('click', '#close', function (evente) {
$("#example").popover('hide');
});
$("#close").click(function(event) {
$("#example").popover('hide');
});
});
<button type="button" id="example" class="btn btn-primary" >click</button>
This is a working solution based on @Chris answer above, but fixed so that upon subsequence clicks of the trigger element, you don't have to click the element twice.
Using .popover('hide)
manually messes up bootstraps internal state, as noted in the comments.
var closePopover = function(eventShown) {
// Set the reference to the calling element
var $caller = $('#' + this.id);
// Set the reference to the popover
var $popover = $('#' + $(eventShown.target).attr('aria-describedby'));
// Unbind any pre-existing event handlers to prevent duplicate clicks
$popover.find('.popover-close').off('click');
// Bind event handler to close button
$popover.find('.popover-close').on('click', function(e) {
// Trigger a click on the calling element, to maintain bootstrap's internal state
$caller.trigger('click');
});
}
$('.popoverTriggerElement').popover({
trigger: 'click'
})
.on('shown.bs.popover', closePopover)
Now, you can use the close button without duplicating the click events, and keeping bootstraps internal state in check so it works as expected.
Put this in your title popover constructor...
'<button class="btn btn-danger btn-xs pull-right"
onclick="$(this).parent().parent().parent().hide()"><span class="glyphicon
glyphicon-remove"></span></button>some text'
...to get a small red 'x' button on top-right corner
//$('[data-toggle=popover]').popover({title:that string here})
I found other answers were either not generic enough, or too complicated. Here is a simple one that should always work (for bootstrap 3):
$('[data-toggle="popover"]').each(function () {
var button = $(this);
button.popover().on('shown.bs.popover', function() {
button.data('bs.popover').tip().find('[data-dismiss="popover"]').on('click', function () {
button.popover('toggle');
});
});
});
Then just add attribute data-dismiss="popover"
in your close button. Also make sure not to use popover('hide')
elsewhere in your code as it hides the popup but doesn't properly sets its internal state in bootstrap code, which will cause issues next time you use popover('toggle')
.
Here's a "cheat" solution:
Follow the usual directions for a dismissable popup.
Then slap an 'X' in the box with CSS.
CSS:
.popover-header::after {
content: "X";
position: absolute;
top: 1ex;
right: 1ex;
}
JQUERY:
$('.popover-dismiss').popover({
trigger: 'focus'
});
HTML:
<a data-toggle="popover" data-trigger="focus" tabindex="0" title="Native Hawaiian or Other Pacific Islander" data-content="A person having origins in any of the original peoples of Hawaii, Guam, Samoa, or other Pacific Islands.">?</a>
Technically speaking that makes it dismissable if someone clicks something other than the "X" but that's not a problem in my scenario at least.