Be able to use existing DOM element as popover content
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");
I agree with this. What you can do is this :
$('#target').popover({
placement: "left",
trigger: "manual",
content: $("#popover-content").parent().html()
}).popover("show");
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.
Good point, I just needed to pass the HTML for my project, so I hadn't thought about that ...
you can just do this:
content: $("#popover-content")
if you don't want to remove the actual node, just call clone on it.
@fat:
content: $("#popover-content")
It does not work for me, can I have more explanations please?
Nah yeah, for anyone who stumbles across this later, this does NOT work. This would be really nice to have,
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.
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/
+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.
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)
+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.
I used the clone method
$('#myPopover').popover({
html: true,
content: function () {
return $("#myPopoverContent").clone();
}
});
Locking this due to its age. Added a note to look at this for v4 though.