bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Be able to use existing DOM element as popover content

Open markmandel opened this issue 14 years ago • 13 comments

Much like how .modal() works, it would be nice if you could pass a dom element into the content option, and have that be the content for the popover.

This would let the popover work nicer with other JS frameworks, such as AngularJS.

I was thinking something like:

$('#target').popover({placement: "left", trigger: "manual", content: $("#popover-content") }).popover("show");

markmandel avatar Jun 05 '12 02:06 markmandel

I agree with this. What you can do is this :

$('#target').popover({
    placement: "left",
    trigger: "manual",
    content: $("#popover-content").parent().html()
}).popover("show");

MGaetan89 avatar Jun 05 '12 10:06 MGaetan89

Yeah, but the issue with using .html() is that it doesn't actually use the DOM element, it uses the HTML from what is there now.

So you can lose everything like event bindings and all sorts of other pieces which can be so useful. This is what causes the issues, and was the impetus behind having this ticket.

markmandel avatar Jun 05 '12 22:06 markmandel

Good point, I just needed to pass the HTML for my project, so I hadn't thought about that ...

MGaetan89 avatar Jun 05 '12 22:06 MGaetan89

you can just do this:

content: $("#popover-content")

if you don't want to remove the actual node, just call clone on it.

fat avatar Jun 08 '12 02:06 fat

@fat:

content: $("#popover-content")

It does not work for me, can I have more explanations please?

jpleclerc avatar Aug 20 '12 22:08 jpleclerc

Nah yeah, for anyone who stumbles across this later, this does NOT work. This would be really nice to have,

cha0s avatar Mar 24 '13 06:03 cha0s

I did get this to work, passing the DOM element. I had a problem since I was cloning from an invisible template element and without calling show() on it first it was (obviously) not showing up. In my previous implementation qTip has shown the entire tree so this hadn't mattered.

CrawfordWCR avatar Apr 09 '13 18:04 CrawfordWCR

Hey just in case anybody finds this and is trying to make it work, you have to set {html: true} in the options.

Here is an example http://jsfiddle.net/nzM6F/

L0g1k avatar May 17 '13 05:05 L0g1k

+1 for introducing this feature in Bootstrap!

As a workaround, I created a generic solution which allows to use the data-content attribute as text value or CSS selector:

$('[data-toggle="popover"]').popover({
    html: true,
    content: function() {
        var content = $(this).data('content');
        try { content = $(content).html() } catch(e) {/* Ignore */}
        return content;
    }
});

Unfortunately, I have to use a try-catch block to avoid jQuery exceptions when trying to look for DOM element(s).

Usage and more information here: http://stackoverflow.com/questions/11349058/how-can-i-use-div-in-twitters-popover-with-multiple-buttons/16638319#16638319 You can test this solution here: http://jsfiddle.net/almeidap/eX2qd/

@CrawfordWCR: this should also work with elements using the hide CSS class.

almeidap avatar May 20 '13 16:05 almeidap

Nice :) You can totally avoid the exception handling stuff like so:

$('[data-toggle="popover"]').popover({
    html: true,
    content: function() {
        var content = $(this).data('content');
        var $content = $(content);
        return $content.length > 0 ? $content.html() : '';
    }
});

I didn't test it, sorry... but the theory should be correct :) The only thing I'm not sure on is, what is supposed to happen if content is a valid selector but selects nothing? (The '' ternary case)

cha0s avatar May 20 '13 21:05 cha0s

+1 for this feature. For anyone else looking to do this, to work around this and keep all of my elements attached events, i was able to do this:

$('#myPopover').popover({
    html: true,
    content: function () {
        return $("#myPopoverContent");
    }
}).on('hidden.bs.popover', function () {
    $("#myPopoverContentContainer").append($("#myPopoverContent"));
});

Basically, the hidden event is called before the popover content is destroyed, so i copy it back to the parent container. Works like a charm. Hopefully this can be introduced by default or as an option in a future revision.

ericklimuk avatar Oct 16 '13 04:10 ericklimuk

I used the clone method

$('#myPopover').popover({
    html: true,
    content: function () {
        return $("#myPopoverContent").clone();
    }
});

juancarloscruzd avatar Aug 29 '14 17:08 juancarloscruzd

Locking this due to its age. Added a note to look at this for v4 though.

cvrebert avatar Aug 29 '14 18:08 cvrebert