You can use the native event of the function and change the contents of the popover. But there are a few different ways to do this, depending on your intent.
Firstly, because it is an image that uses the <img>
tag, it is necessary to add the data-html="true"
attribute in the link, so that the content of the popover is interpreted as HTML rather than pure text:
<a href="#" data-html="true"...>
Also add another data-img
attribute with path to image:
<a href="#" data-html="true" data-img="url_da_imagem"...>
When the show.bs.popover
event (event that is triggered when displaying the popover at the click of the link) is called, you change the data-content
attribute (that is popover content) dynamically by adding the <img>
tag % and drawing 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><scriptsrc="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 data-content
attribute, but for a better visualization of the code, it is much better to use the show.bs.popover
event, since you can even create a template and insert it into the popover:
$(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><scriptsrc="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 data-content
attribute of the <a>
link.