Vishnu Vishnu - 28 days ago 14
CSS Question

Bootstrap: popover button click

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>




Answer

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');
        }
    });
});
Comments