Bootstrap popover content cannot changed dynamically

后端 未结 14 2208
旧时难觅i
旧时难觅i 2020-11-29 22:40

I use the code as follows:

$(\".reply\").popover({
  content: \"Loading...\",
  placement: \"bottom\"
});

$(\".reply\").popover(\"toggle\");
相关标签:
14条回答
  • 2020-11-29 23:24

    This answer from 2012 does not work with Bootstrap 3 popovers. I extracted a working solution from this question:

    $("#popover").attr('data-content', 'new content');

    0 讨论(0)
  • 2020-11-29 23:27

    If you grab the popover instance like this:

    var popover = $('.reply').data('bs.popover');
    

    Then, to redraw the popover, use the .setContent() method:

    popover.setContent();
    

    I found out browsing the source: https://github.com/twitter/bootstrap/blob/master/js/popover.js

    So, in your example, try:

    thisVal.attr('data-content',data).data('bs.popover').setContent();
    

    Update

    The setContent() method also removes the placement class, so you should do:

    var popover = thisVal.attr('data-content',data).data('bs.popover');
    popover.setContent();
    popover.$tip.addClass(popover.options.placement);
    

    Demo: http://jsfiddle.net/44RvK

    0 讨论(0)
  • 2020-11-29 23:29

    SIMPLE SOLUTION

    You can try with this :

    //Bootstrap v3.3.7 var popoverEl = $("#popover"); popoverEl.attr("data-content", "NEW CONTENT"); popoverEl.popover("show");

    Thanks.

    0 讨论(0)
  • 2020-11-29 23:31

    you can just pass the title as function

    var content = 'Loading...'
    
    function dynamicContent(){
      return content
    }
    $(".reply").popover({
      content: dynamicContent,
      placement: "bottom"
    });
    
    $(".reply").popover("toggle");
    

    and then change the variable content dynamically.

    0 讨论(0)
  • 2020-11-29 23:34

    When a popover is enabled, it retains its previous version(if it was created already) even if the data is change. You need to destroy the previous version to allow for the new popover to be created again. In Bootstrap 4.x do it by

    .popover('dispose')
    

    do it whenever there is a change in data through an ajax call.

    0 讨论(0)
  • 2020-11-29 23:35

    In bootstrap 3:

    if($el.data('bs.popover')) {
        $el.data('bs.popover').options.content = "New text";
    }
    $el.popover('show');
    
    0 讨论(0)
提交回复
热议问题