0

I have used bootstrap popover function, its working now.. but i need to "dismiss" popover box if i click anywhere in the page. please check the code that i have used.

First click on the button then i need to "dismiss" popover box on clicking the black space (now its dismiss only when we clicked on the same button).

$(document).ready(function() {
  $('[data-toggle="popover"]').popover();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="width:400px; height:400px; background:#000;padding-top:50px;">
  <a href="#" title="Header" data-toggle="popover" data-content="Some content" class="btn btn-danger">Click here</a>
  <br>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
vishnu
  • 2,848
  • 3
  • 30
  • 55
  • 2
    Look here - http://stackoverflow.com/questions/11703093/how-to-dismiss-a-twitter-bootstrap-popover-by-clicking-outside – TT8 Nov 07 '16 at 10:12
  • http://stackoverflow.com/questions/8947749/how-can-i-close-a-twitter-bootstrap-popover-with-a-click-from-anywhere-else-on – prasanth Nov 07 '16 at 10:16

3 Answers3

2

Just use data-trigger="focus" in a tag

For more info check Dismissible popover

$(document).ready(function() {
  $('[data-toggle="popover"]').popover();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="width:400px; height:400px; background:#000;padding-top:50px;">
  <a href="#" title="Header" data-toggle="popover" data-content="Some content" class="btn btn-danger" data-trigger="focus">Click here</a>
  <br>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Abhishek Pandey
  • 13,302
  • 8
  • 38
  • 68
1

This will work

Use below javascript for dismiss popover when outside click

$('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');
        }
    });
});
1

Try this

$(document).ready(function(){
 $('[data-toggle="popover"]').popover({trigger:'focus'});
});

$(document).ready(function(){
 $('[data-toggle="popover"]').popover({trigger:'focus'});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>





<div class="container" style="width:400px; height:400px; background:#000;padding-top:50px;">
  <a href="#" title="Header" data-toggle="popover" data-content="Some content" class="btn btn-danger">Click here</a><br>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Jinu Kurian
  • 9,128
  • 3
  • 27
  • 35