Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close / dismiss Bootstrap Popover when clicking the popover trigger element?

jsFiddle: http://jsfiddle.net/kAYyR/

Screenshot:

screenshot

Here's what works:

  1. Open popover on button click
  2. Close popover on click outside popover
  3. Close popover on click of .close button

BUT... I cannot get the popover to close when you click the original button again. Instead the popover flashes off and on again.

Duplicate it yourself here.

How can I accomplish this?

HTML:

<button id="popoverId" class="popoverThis btn btn-large btn-danger">Click to toggle popover</button>
<div id="popoverContent" class="hide">This <em>rich</em> <pre>html</pre> content goes inside popover</div>

JS:

$('#popoverId').popover({
    html: true,
    title: "Popover Title",
    content: function () {
        return $('#popoverContent').html();
    }
});


var isVisible = false;
var clickedAway = false;

$('.popoverThis').popover({
    html: true,
    trigger: 'manual'
}).click(function (e) {
    $(this).popover('show');
    $('.popover-content').append('<a class="close" style="position: absolute; top: 0; right: 6px;">&times;</a>');
    clickedAway = false
    isVisible = true
    e.preventDefault()
});

$(document).click(function (e) {
    if (isVisible & clickedAway) {
        $('.popoverThis').popover('hide')
        isVisible = clickedAway = false
    } else {
        clickedAway = true
    }
});
like image 621
Ryan Avatar asked Apr 11 '13 09:04

Ryan


2 Answers

Do you want work like this ?

http://jsfiddle.net/kAYyR/3/

$('#popoverId').popover({
    html: true,
    title: 'Popover Title<a class="close" href="#");">&times;</a>',
    content: $('#popoverContent').html(),
});

$('#popoverId').click(function (e) {
    e.stopPropagation();
});

$(document).click(function (e) {
    if (($('.popover').has(e.target).length == 0) || $(e.target).is('.close')) {
        $('#popoverId').popover('hide');
    }
});
like image 128
Steely Wing Avatar answered Oct 12 '22 17:10

Steely Wing


I use this:

    $('[data-toggle="popover"]').popover({html: true, container: 'body'});

    $('[data-toggle="popover"]').click(function (e) {
        e.preventDefault();
        $('[data-toggle="popover"]').not(this).popover('hide');
        $(this).popover('toggle');
    });

    $(document).click(function (e) {
        if ($(e.target).parent().find('[data-toggle="popover"]').length > 0) {
            $('[data-toggle="popover"]').popover('hide');
        }
    });
like image 23
Cam Tullos Avatar answered Oct 12 '22 18:10

Cam Tullos