Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap popover, hide on click outside?

using bootstrap popover, and now im trying to get this code to click outside the popover to close the popover:

$('body').on('click', function (e) {     $('[data-toggle="popover"]').each(function () {         //the 'is' for buttons that trigger popups         //the 'has' for icons within a button that triggers a popup         if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {             $(this).popover('hide');         }     }); }); 

But when i use this part, i can close the popover but i cant click the other buttons, anyone got any idea how i can do that?

All the buttons:

<a href="#" class="btn btn-xs btn-primary" data-toggle="popover">This opens popover</a> <a href="#" class="btn btn-xs btn-primary">Other link</a> <- Doesn't work  <a href="#" class="btn btn-xs btn-primary">Other link</a> <- Doesn't work  
like image 352
Tommy Avatar asked Dec 09 '13 09:12

Tommy


People also ask

How do you hide a popover?

To hide the displayed popover, use the popover(“hide”) method.

How do I show popover on hover?

Set the trigger option of the popover to hover instead of click , which is the default one. Or with an initialization option: $("#popover"). popover({ trigger: "hover" });

What is ngbPopover?

ngbPopover. The string content or a TemplateRef for the content to be displayed in the popover. If the title and the content are falsy, the popover won't open.


2 Answers

I found this : http://bootply.com/99372

$('body').on('click', function (e) {     $('[data-toggle=popover]').each(function () {         // hide any open popovers when the anywhere else in the body is clicked         if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {             $(this).popover('hide');         }     }); }); 

It's almost the same code as you...

like image 104
BENARD Patrick Avatar answered Oct 18 '22 21:10

BENARD Patrick


Just add this element to close the popover on next click.

data-trigger="focus"  

Reference from here: Bootstrap Popover

like image 35
ravi Avatar answered Oct 18 '22 23:10

ravi