I needed to find something that would work for several popovers in Bootstrap 3 as well.
Here is what I did:
I had several elements for which I wanted to use popover, so I did not want to use identifiers.
The markup can 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 contents of 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 javascript for all three buttons:
This method works when the container is not attached to the body by default.
$('.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 the body
Then use the described aria 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'); }); });
Chris Nov 10 '13 at 15:28 2013-11-10 15:28
source share