Reputation: 4041
I am using twitter's bootstrap's popover here. Right now, when i scroll over the popover text a popover appears with just text from the <a>
's data-content
attribute. I was wondering if there was anyway to put a <div>
inside the popover. Potentially, I would like to use php and mysql in there, but if i could get a div to work i think i can figure out the rest. I tried setting data-content to a div
ID, but it didnt work.
HTML:
<a class='danger'
data-placement='above'
rel='popover'
data-content='#PopupDiv'
href='#'>Click</a>
Upvotes: 152
Views: 141633
Reputation: 4565
First of all, if you want to use HTML inside the content you need to set the HTML option to true:
$('.danger').popover({ html : true});
Then you have two options to set the content for a Popover
Using data-content: You need to escape the HTML content, something like this:
<a class='danger' data-placement='above'
data-content="<div>This is your div content</div>"
title="Title" href='#'>Click</a>
You can either escape the HTML manually or use a function. I don't know about PHP but in Rails we use html_safe.
Using a JS function: If you do this, you have several options. The easiest I think is to put your div content hidden wherever you want and then write a function to pass its content to popover. Something like this:
$(document).ready(function(){
$('.danger').popover({
html : true,
content: function() {
return $('#popover_content_wrapper').html();
}
});
});
And then your HTML looks like this:
<a class='danger' data-placement='above' title="Popover Title" href='#'>Click</a>
<div id="popover_content_wrapper" style="display: none">
<div>This is your div content</div>
</div>
PS: I've had some troubles when using popover and not setting the title attribute... so, remember to always set the title.
Upvotes: 308
Reputation: 6030
All of these answers miss a very important aspect!
By using .html or innerHtml or outerHtml you are not actually using the referenced element. You are using a copy of the element's html. This has some serious draw backs.
What you want to do is load the object itself into the popover.
https://jsfiddle.net/shrewmouse/ex6tuzm2/4/
HTML:
<h1> Test </h1>
<div><button id="target">click me</button></div>
<!-- This will be the contents of our popover -->
<div class='_content' id='blah'>
<h1>Extra Stuff</h1>
<input type='number' placeholder='number'/>
</div>
JQuery:
$(document).ready(function() {
// We don't want to see the popover contents until the user clicks the target.
// If you don't hide 'blah' first it will be visible outside of the popover.
//
$('#blah').hide();
// Initialize our popover
//
$('#target').popover({
content: $('#blah'), // set the content to be the 'blah' div
placement: 'bottom',
html: true
});
// The popover contents will not be set until the popover is shown. Since we don't
// want to see the popover when the page loads, we will show it then hide it.
//
$('#target').popover('show');
$('#target').popover('hide');
// Now that the popover's content is the 'blah' dive we can make it visisble again.
//
$('#blah').show();
});
Upvotes: 6
Reputation: 17
here is an another example
<a data-container = "body" data-toggle = "popover" data-placement = "left"
data-content = "<img src='<?php echo baseImgUrl . $row1[2] ?>' width='250' height='100' ><div><h3> <?php echo $row1['1'] ?></h3> <p> <span><?php echo $countsss ?>videos </span>
<span><?php echo $countsss1 ?> followers</span>
</p></div>
<?php echo $row1['4'] ?> <hr><div>
<span> <button type='button' class='btn btn-default pull-left green'>Follow </button> </span> <span> <button type='button' class='btn btn-default pull-left green'> Go to channel page</button> </span><span> <button type='button' class='btn btn-default pull-left green'>Close </button> </span>
</div>">
<?php echo $row1['1'] ?>
</a>
Upvotes: -2
Reputation: 83
In addition to other replies. If you allow html
in options you can pass jQuery
object to content, and it will be appended to popover's content with all events and bindings. Here is the logic from source code:
html
is not allowed content data will be applied as texthtml
allowed and content data is string it will be applied as html
$("#popover-button").popover({
content: $("#popover-content"),
html: true,
title: "Popover title"
});
Upvotes: 8
Reputation: 404
Late to the party. Building off the other solutions. I needed a way to pass the target DIV as a variable. Here is what I did.
HTML for Popover source (added a data attribute data-pop that will hold value for destination DIV id/or class):
<div data-html="true" data-toggle="popover" data-pop="popper-content" class="popper">
HTML for Popover content (I am using bootstrap hide class):
<div id="popper-content" class="hide">Content goes here</div>
Script:
$('.popper').popover({
placement: popover_placement,
container: 'div.page-content',
html: true,
trigger: 'hover',
content: function () {
var pop_dest = $(this).attr("data-pop");
//console.log(plant);
return $("#"+pop_dest).html();
}});
Upvotes: 11
Reputation: 61083
Building on jävi's answer, this can be done without IDs or additional button attributes like this:
http://jsfiddle.net/isherwood/E5Ly5/
<button class="popper" data-toggle="popover">Pop me</button>
<div class="popper-content hide">My first popover content goes here.</div>
<button class="popper" data-toggle="popover">Pop me</button>
<div class="popper-content hide">My second popover content goes here.</div>
<button class="popper" data-toggle="popover">Pop me</button>
<div class="popper-content hide">My third popover content goes here.</div>
$('.popper').popover({
container: 'body',
html: true,
content: function () {
return $(this).next('.popper-content').html();
}
});
Upvotes: 42
Reputation: 1318
Another alternate method if you wish to just have look and feel of pop over. Following is the method. Offcourse this is a manual thing, but nicely workable :)
HTML - button
<button class="btn btn-info btn-small" style="margin-right:5px;" id="bg" data-placement='bottom' rel="tooltip" title="Background Image"><i class="icon-picture icon-white"></i></button>
HTML - popover
<div class="bgform popover fade bottom in">
<div class="arrow"></div>
..... your code here .......
</div>
JS
$("#bg").click(function(){
$('.bgform').slideToggle();
});
Upvotes: 11