You can use the function’s native event and change the contents of the Popover. But there are a few different ways to do this depending on your intention.
First, because it is an image that uses the tag <img>
, it is necessary to add the attribute data-html="true"
in the link, so that the content of the Popover is interpreted as HTML and not plain text:
<a href="#" data-html="true"...>
Also add another attribute data-img
with the way to image:
<a href="#" data-html="true" data-img="url_da_imagem"...>
When the event show.bs.popover
(event that is triggered when displaying the Popover at the link click) is called, you change the attribute data-content
(which is the contents of Popover) dynamically, adding the tag <img>
and pulling the path of the image in data-img
.
In the example below the Popover already has the text. I will just insert an image after the text concatenating the existing text with the tag <img>
:
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
$('[data-toggle="popover"]').on('show.bs.popover', function(){
// conteúdo original do popover
var conteudo = $(this).data("content");
// imagem a ser inserida
var imagem = '<div><img width="50" src="'+ $(this).data('img') +'"><div>';
// adiciona uma imagem concatenando com o que já tinha
$(this).attr("data-content", conteudo+imagem);
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<h3>Popover Example</h3>
<a href="#" data-html="true" data-img="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Toggle popover</a>
</div>
You can also insert everything directly into the attribute data-content
, but for a better view of the code, it is much better to use the event show.bs.popover
, because you can even assemble a template and insert it in Portpover:
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
$('[data-toggle="popover"]').on('show.bs.popover', function(e){
// novo conteúdo para o popover
var conteudo = 'Some content inside the popover<br>'
+'<img width="50" src="'+ $(this).data('img') +'">';
// adiciona o conteúdo da variável "conteudo"
$(this).attr("data-content", conteudo);
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<h3>Popover Example</h3>
<a href="#" data-html="true" data-img="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" data-toggle="popover" title="Popover Header">Toggle popover</a>
</div>
In this case above, you can even omit the attribute data-content
of the link <a>
.
Like this: https://bootsnipp.com/snippets/Ek2nl
– Edjane