Change Twitter Bootstrap Tooltip content on click

前端 未结 25 1019
情深已故
情深已故 2020-11-27 10:02

I have a tooltip on an anchor element, that sends an AJAX request on click. This element has a tooltip (from Twitter Bootstrap). I want the tooltip content to change when th

相关标签:
25条回答
  • 2020-11-27 10:19

    In Bootstrap 3 it is sufficient to call elt.attr('data-original-title', "Foo") as changes in the "data-original-title" attribute already trigger changes in the tooltip display.

    UPDATE: You can add .tooltip('show') to show the changes immediately, you need not to mouseout and mouseover target to see the change in the title

    elt.attr('data-original-title', "Foo").tooltip('show');
    
    0 讨论(0)
  • 2020-11-27 10:19

    you can update the tooltip text without actually calling show/hide:

    $(myEl)
        .attr('title', newTitle)
        .tooltip('fixTitle')
        .tooltip('setContent')
    
    0 讨论(0)
  • 2020-11-27 10:20

    I couldn't get any of the answers working, here's a workaround:

    $('#'+$(element).attr('aria-describedby')+' .tooltip-inner').html(newTitle);
    
    0 讨论(0)
  • 2020-11-27 10:20

    you can use this code to hide the tool tip change its title and show the tooltip again, when the ajax request returns successfully.

    $(element).tooltip('hide');
    $(element).attr('title','this is new title');
    $(element).tooltip('fixTitle');
    setTimeout(function() {  $(element).tooltip('show');}, 500);
    
    0 讨论(0)
  • 2020-11-27 10:23

    This worked for me: (bootstrap 3.3.6; jquery=1.11.3)

    <a id="alertTooltip" href="#" data-html="true" class="tooltip" data-toggle="tooltip" title="Tooltip message"></a>
    
    <script>
      $('#alertTooltip').attr('title', "Tooltip new <br /> message").tooltip('fixTitle');
    </script>
    

    The attribute data-html="true" allow to use html on the tooltip title.

    0 讨论(0)
  • 2020-11-27 10:24
    $(this).attr('data-original-title', 'click here to publish')
           .tooltip('fixTitle')
           .tooltip('show');
    

    Useful if you need to change a tooltip's text after it has been initialized with attr 'data-original-title'.

    0 讨论(0)
提交回复
热议问题