Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Bootstrap popover to a specific div element

I am trying to add a bootstrap popover to a div element that contains arbitrary content like so (but what I came up with doesn't work):

<div id="popover-target">
   <h3>I need a popover</h3>
      <p>
        This is some content for the section
        that needs to be explained by the popover.
      </p>
</div>
<button class="btn">Show popover</button>

<script>
    $(function(){
        $('.btn').click(function(){
            $('#popover-target').popover({title: 'Title', content: 'Test Content', container: 'body', placement: 'right'});
        });
    });
</script>

If I change $('#popover-target') to $('.btn') then the popover displays correctly. Any ideas?

like image 622
Maxence Coulibaly Avatar asked Sep 18 '25 22:09

Maxence Coulibaly


1 Answers

You have just initialized the popover on button click which should work fine, if you hover over the #popover-target div after button click. If you want to show it after button click with hovering over it, you can use .popover('show') like:

$('.btn').click(function() {
  $('#popover-target').popover({
      trigger: 'hover',
      title: 'Title',
      content: 'Test Content',
      placement: 'bottom'
    })
    .popover('show');
});
like image 194
palaѕн Avatar answered Sep 20 '25 12:09

palaѕн