Bootstrap Popover
I would like to be able to click outside the popover to make it disappear.
This code works well - it closes one popover when the other opens, and when you close the button again, it goes away.
var $visiblePopover;
$('body').on('click', '[rel="popover"]', function() {
var $this = $(this);
// check if the one clicked is now shown
if ($this.data('popover').tip().hasClass('in')) {
// if another was showing, hide it
$visiblePopover && $visiblePopover.popover('hide');
// then store reference to current popover
$visiblePopover = $this;
} else { // if it was hidden, then nothing must be showing
$visiblePopover = '';
}
});
I need to keep this current functionality, but change it so that it does the same when you step outside of the popover.
+3
source to share
5 answers
$.fn.modal.Constructor = Modal
$(function () {
$('body').on('click.modal.data-api', '[data-toggle="modal"]', function (e) {
if($visiblePopover && $visiblePopover){
alert("HIDE POPOVER WHEN MODAL IS OPENED")
$visiblePopover && $visiblePopover.popover('hide');
}
var $this = $(this),
href = $this.attr('href'),
$target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))),
option = $target.data('modal') ? 'toggle' : $.extend({
remote: !/#/.test(href) && href
}, $target.data(), $this.data())
e.preventDefault()
$target.modal(option).one('hide', function () {
$this.focus()
})
})
})
0
source to share
add this code to the end of bootstrap.min.js
1) 2 popover were clicks, always hid the starting population
2) the click outside the popover will hide as well.
$count=0;$(document).click(function(evt){if($count==0){$count++;}else{$('[data-toggle="popover"]').popover('hide');$count=0;}});$('[data-toggle="popover"]').popover();$('[data-toggle="popover"]').on('click', function(e){$('[data-toggle="popover"]').not(this).popover('hide');$count=0;});
0
source to share