Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Popover

I would like to be able to click outside the popover to make it dissapear.

This code is working well - closes one popover when another is opened and ofcourse when you clcik 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 modify it so that it does the same thing for when you click outside the popover as well.

like image 213
user2075439 Avatar asked Jun 19 '26 01:06

user2075439


2 Answers

you can do it simply by adding:

$('html').click(function(e) {
     $('.btn').popover('hide');
});

jsfiddle

like image 93
PersianMan Avatar answered Jun 23 '26 19:06

PersianMan


This little trick is handy if you want to close all other popovers except the one that has been clicked on:

$('.popover').click(function (evt) {
    evt.stopPropagation();
    $('.popover').not(this).popover('hide');
});
like image 43
csharpsql Avatar answered Jun 23 '26 18:06

csharpsql



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!