Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Popover content clickable when hidden

My site has multiple popovers over a map (just a div with background image) with clickable icons:

When clicking those icons, a popover is shown. This popover has a carousel that contains text with links:

When I click outside the popover, all popovers are hidden. This supposedly works ok. I got the code from this Stack Overflow question.

But the real issue occurs when you click outside the popover to hide it again. The popover is hidden, but it's still in the DOM. This means the links are still clickable!

When I click the plus icon to hide the popover, the popover is hidden (removed?), but when clicking outside the plus (i.e. anywhere on the page), the popover is still present (but not visible) right above </body>.

Please help! This annoys the hell out of me.. :(

Edit: Might be worth mentioned that the popovers are dynamically added:

$('.plus').each(function(i) {

    var $self = $(this);

    $self.popover({
        html: true,
        title: false,
        placement: function() {
            return 'auto left'; // TODO: calculate placing
        },
        content: function() {

            var html   = '<div id="carousel-' + i + '" class="carousel slide">';
            var list   = '<ol class="carousel-indicators">';
            var slides = '<div class="carousel-inner">';

            var count = 0;

            $('.post[data-category="' + $(this).data('category') + '"]').each(function(j) {

                // add indicator for slide
                list += '<li data-target="#carousel-' + i + '" data-slide-to="' + count + '"' + (count == 0 ? ' class="active"' : '') + '></li>';

                // add html for slide
                slides += '<div class="item' + (count == 0 ? ' active' : '') + '">' + $(this).html() + '</div>';

                // increase counter
                count++;

            });

            // merge all html
            html += list + '</ol>' + slides + '</div></div>';

            return html;
        }
    });
like image 430
rebellion Avatar asked Sep 01 '13 21:09

rebellion


People also ask

How do I customize bootstrap popover?

To create a popover, you need to add the data-bs-toggle="popover" attribute to an element. Whereas, popover's title and its content that would display upon trigger or activation can be specified using the title and data-bs-content attribute respectively. Here is the standard markup for adding a popover to a button.

What is the difference between Tooltip and popover?

Tooltip: use tooltips to show a short text to respondents when they hover over a word or icon. Popover: use popovers to show a longer text, or when you want to have a link to an external web page. It is shown when the respondent clicks on a word or icon.

How do I enable popovers in bootstrap?

To create a popover, add the data-toggle="popover" attribute to an element. Note: Popovers must be initialized with jQuery: select the specified element and call the popover() method.


2 Answers

I had a similar issue, big headache, and searching I got to this page:

https://github.com/twbs/bootstrap/issues/4215

Apparently when you add Popovers to dynamic content you need to add this option:

selector: '[rel="popover"]'

in your case it would be

$self.popover({
    selector: '[rel="popover"]'
    html: true,
    title: false,
    ...

This resolved all my "hidden popover clicks detection" issues.

like image 121
user2744441 Avatar answered Sep 29 '22 22:09

user2744441


This is what I did in order to prevent elements within the hidden popover from being clicked

$('button.new-history').on('hidden.bs.popover', function () {
    $(this).next().remove();
})

The idea being that when the popover is hidden, you should remove it from the DOM.

Hope it helps!

like image 43
Salman Hasrat Khan Avatar answered Sep 29 '22 23:09

Salman Hasrat Khan